I would like to ask another question about how to write a method that generates random words of a certain length and when calling a method user selects the length of words that will be generated and returned from the method. JOptionPane needs to be used for input. The method work needs to be shown through Main.
Asked
Active
Viewed 8,910 times
0
-
Do they have to be dictionary words (like an English dictionary) or a random set of characters? What have you tried so far? – mkobit Dec 11 '14 at 17:50
-
They have to be a random set of characters of a certain length. User needs to select the length of words that will be generated. – Stefan Aritonović Dec 11 '14 at 17:53
-
Totally possible. If you want to ensure it's random in capitalization to, you could just randomly grab from the [ASCII table](http://www.asciitable.com) by generating random numbers within that range, then casting it to `char` (65 to 90 represents capital letters A to Z, 97 to 122 represents lowercase a to z). If not, you need to create an array of letters and randomly choose from it – Vince Dec 11 '14 at 17:59
-
What have you done so far? For example, what are "words"? For this exercise that are a collection of (some Locale) characters separated by whitespace. So, start there. See if you can write a simple method that generates fixed-length strings based on random characters. Hint: Generating a series of number between 1 and 26 (or go zero-based if you like), inclusive, that you use to find a character within an array of all characters might be a way to start cheaply. – Dec 11 '14 at 18:02
-
possible duplicate of [Generating random words in NetBeans IDE?](http://stackoverflow.com/questions/27386578/generating-random-words-in-netbeans-ide) – Joe Dec 12 '14 at 10:10
3 Answers
1
What kind of words do you want to generate? Random lowercase characters?
String getRandomWord(int length) {
String r = "";
for(int i = 0; i < length; i++) {
r += (char)(Math.random() * 26 + 97);
}
return r;
}

Quirin Schweigert
- 11
- 1
1
Here is a simple way to generate lowercase strings of length
characters. The idea is that you randomly add a character using the ASCII table up to the required length.
public static String randomWord(int length) {
Random random = new Random();
StringBuilder word = new StringBuilder(length);
for (int i = 0; i < length; i++) {
word.append((char)('a' + random.nextInt(26)));
}
return word.toString();
}

mkobit
- 43,979
- 12
- 156
- 150
-
But the user needs to choose the length of characters that will be generated and returned from the method. I have to use JOptionPane for input. – Stefan Aritonović Dec 11 '14 at 18:06
-
@StefanAritonović This is the method you would call to generate a random word. You need to write the code that hooks up a `JOptionPane` and whatever else your requirements are. – mkobit Dec 11 '14 at 18:13
0
The easiest way is to use RandomStringUtils class from org.apache.commons.lang3 package. The example below shows you, how it can be used. It will return something like "WYhZXwUQfl", when you pass, for example, 10 to the method.
public String generateRandomString(int stringLength){
return RandomStringUtils.randomAlphabetic(stringLength);
}
I encourage you to check this class, because it has many useful methods to generate random strings.

Ivan Polovyi
- 94
- 3
- 5
-
https://stackoverflow.com/help/how-to-answer, Please add more details to your answer. – yusuf hayırsever Feb 25 '21 at 06:35