This video shows one way of many ways to construct a random string in Java. The concepts in this video are taken from the first few videos in my "Java Tutorials for Beginners" playlist. Thank you for your questions! Hope this helps!
I had problems with the initialize of the variable i. I use a text editor and the prompt command at linux and it says that cannot find the variable i. I've write it like it was in the video. What is the matter where?? I appreciate your help, thx!!!
The modified version of your code generates a random string from 0 - 15. How would you go about it, if you wanted your code to generate a random string that has a character length of 14 -19?
Because String objects are immutable, each concatenation is going to create a whole new String, which is a bad habit to build if you have any intention of doing anything large-scale. There is a String(char[]) constructor, which can do what that second loop does rather more cleanly. It’s also a waste of time to create a random string of zero length, so line 15 in your modified version should be int length = rand.nextInt(14) + 1; , which will produce a random string of length 0
thank you so much for the video andrew ! i'm sorry to ask you again. i've got one more question, if i got an array of string like this : String [ ] symbols = {"heart", "diamond", "clubs", "spades"}; how can i random the values of the array ? i've tried to loop the values and then stored it on an array list, but still i've got incompatible types message. here's my code : String [] symbol = {"heart" , "diamond" , "clubs" , "spades"}; Random rand = new Random(); ArrayList store = new ArrayList (); for(int i = 0; i < symbol.length; i++ ){ store.add(rand.nextInt(symbol[i])); } could you help for this ? sorry to make trouble andrew, thank you so much for the tutorial once again :))
String[] symbol = {"thing1", "thing2", "thing2"}; Random rand = new Random(); System.out.println(symbol[rand.nextInt(symbol.length)]); Something like that. You dont need to make another Array for that, or I just dont know what you want to achive. and instead of System.out you can just write something like String CPU = symbol[rand.nextInt(symbol.length)]; And that's it. If its for the game I quess..
You could also have skipped creating the array in that case. Simply make the contents of the first loop randomString += characters.charAt(rand.nextInt(characters.length()));
That would once again have the problem of creating myriads of superfluous String instances, so it’s still better to build the array and then use the String constructor overload with the char array input. This also avoids creating the superfluous empty String at the start.
can we generate all possible combination throw random. you are printing only one combination. but i want to print all possible combination. like(ABCD, ABCE,ACED..............) SO ON. CAN YOU HELP ME ON THIS.
This makes sense but does not solve what is expected of me with my project currently. Teacher wants me to have 3 words and then generate random output of the 3 words
Instead of the full alphabet, you could set the list of characters to be the three words. This can be done dynamically from user input or hard-coded prior to execution.
Laurent de Laprade Thanks for your constructive feedback. However this tutorial set is intended for programmers who are at the beginner level. Hence I used concepts only covered in the beginning stages of my java tutorials. Additionally, understanding the fundamental concepts of programming is much more important for a beginner programmer than being the most efficient. Efficiency comes with knowledge and experience... not java tutorial videos.