Random word from array list
23,586
This will help you I think It's able to get a random word from an array of strings
private static String[] names = { "Terminator", "Slicer","Ninja", "cow", "Robot", "littlegirl" };
name = names[(int) (Math.random() * names.length)];
System.out.println(name);

Author by
Admin
Updated on November 25, 2022Comments
-
Admin 6 months
I'm having an issue initiating a random word from the array. I'm not sure how to refer to the words arraylist to fetch from it. Can someone put me in the right direction for my getRandomWord class? Thanks!
A method getRandomWord which takes nothing as input and returns a random String from words. Remember that you can use the Random class to do this.
import java.io.*; import java.util.*; import java.util.Random; public class WordList{ private ArrayList<String> words; //Construct String from file public static void constructor(String filename) throws IOException{ ArrayList words = new ArrayList(); BufferedReader read = new BufferedReader(new FileReader("filename")); String line = read.readLine(); while (line != null){ words.add(line); //line = reader.readline(); } } public static void getRandomWord(){ Random rand = new Random(); String randomWord = words.get(rand.nextInt(words.size)); } }
-
answerSeeker over 9 yearsSo you're trying to get a random word from a file and output it?
-
Hollis Waite over 9 yearsMember variables (e.g. 'words') cannot be referenced from static methods (e.g. 'getRandomWord()'). Either declare 'words' as static or remove 'static' keyword from method signature.
-
Clad Clad over 9 yearsyou should use something like : int rand= minimum + (int)(Math.random()*maximum);
-
answerSeeker over 9 yearsTry this link maybe it will help you here
-
Admin over 9 years@TatakaiWasumi Indeed. From what I understand the first part of my code takes words from a file and puts them in an arraylist. The second part of my code needs to generate a random number and from that, output the corresponding word in the arraylist. I'm having issues with the fact that words is private... Says I can't reach it from getRandomWord
-
answerSeeker over 9 yearsOk, thanks for the clearing that out
-
answerSeeker over 9 yearswhat happens when you make your array list static?
public static ArrayList<String> words;
` -
Holger over 9 yearsYour method lacks a return statement. And the return type; it should be
String
instead ofvoid
. -
Admin over 9 years@TatakaiWasumi Thank you, that helped! I'm getting closer... Now the error I get has to do with the size of arraylist words:"Size has private access in java.util.arraylist"in my getRandomWord class.
-
answerSeeker over 9 yearsDid you already figure it out? if not, what do u have in main()?
-
Holger over 9 years@user3062703: Use the
size()
method, not the private field.
-
-
Admin over 9 yearsGot it! Thank you @tatakai wasumi
-
answerSeeker over 9 yearsNo problem. I hope your issue is fixed
-
jobukkit almost 9 years@user3062703 You should click the checkmark to indicate that this answer solved your question.