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);
Share:
23,586
Admin
Author by

Admin

Updated on November 25, 2022

Comments

  • Admin
    Admin over 1 year

    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
      answerSeeker over 10 years
      So you're trying to get a random word from a file and output it?
    • Hollis Waite
      Hollis Waite over 10 years
      Member 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
      Clad Clad over 10 years
      you should use something like : int rand= minimum + (int)(Math.random()*maximum);
    • answerSeeker
      answerSeeker over 10 years
      Try this link maybe it will help you here
    • Admin
      Admin over 10 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
      answerSeeker over 10 years
      Ok, thanks for the clearing that out
    • answerSeeker
      answerSeeker over 10 years
      what happens when you make your array list static? public static ArrayList<String> words; `
    • Holger
      Holger over 10 years
      Your method lacks a return statement. And the return type; it should be String instead of void.
    • Admin
      Admin over 10 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
      answerSeeker over 10 years
      Did you already figure it out? if not, what do u have in main()?
    • Holger
      Holger over 10 years
      @user3062703: Use the size() method, not the private field.
  • Admin
    Admin over 10 years
    Got it! Thank you @tatakai wasumi
  • answerSeeker
    answerSeeker over 10 years
    No problem. I hope your issue is fixed
  • jobukkit
    jobukkit almost 10 years
    @user3062703 You should click the checkmark to indicate that this answer solved your question.