0

I want to get a random word from the ArrayList in the method getRandom. My ArrayList is loaded from a file. I am then going to use this word in a hangman game. So I would like it to be printed like *****

import java.util.*;
import java.io.*;


public class Application {
    private ArrayList<Pirateword> piratewords;
    private Scanner input;

    public Application(){
        input=new Scanner(System.in);
        piratewords=new ArrayList<Pirateword>();

    }

    public void runApplication() throws IOException {

        String response;
        String w;
        do {

            load("piratewords.txt");
            save("piratewords.txt");
            response=input.nextLine();


    } while (!((response.equals("q")|| (response.equals("q")))));
        System.out.println("Thank you for playing");
    }




    public void load(String fileName) throws IOException{
        Scanner infile =new Scanner(new InputStreamReader(new FileInputStream(fileName)));
        int num=infile.nextInt();infile.nextLine();
        for (int i=0;i<num;i++) {
            String w=infile.nextLine();
            Pirateword p=new Pirateword(w);
            piratewords.add(p);
        }
        infile.close();
    }

    public void save(String fileName) throws IOException{
        PrintWriter outfile = new PrintWriter(new OutputStreamWriter(new FileOutputStream(fileName)));
        outfile.println(piratewords.size());
        for (Pirateword p:piratewords) {
            outfile.println(p.toString());
        }
        outfile.close();
    }

    public void getRandom() {

}
}
user3430741
  • 25
  • 1
  • 7
  • 1
    do you know how to generate random number? if yes then generate a random then get the value of that random index – stinepike Apr 12 '14 at 16:10
  • @StinePike what if the random number is bigger than the size of the arraylist ? You should mention that the random number must be in the range : 0 > random_number > size of array. –  Apr 13 '14 at 17:13

3 Answers3

1
public String getRandom() {
    return piratewords.get(new Random().nextInt(piratewords.size());
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

Add this to your constructor.

Random gen = new Random();

In your method, use that to return random string among your arraylist

public String getRandom() {
    int index = gen.nextInt(piratewords.size());
    return piratewords.get(index);
}
GoldRoger
  • 1,263
  • 7
  • 10
0

Try with Collections.shuffle() if there is no harm to shuffle the items in original list.

Note: Don't use it if there is a large number of items in the list.

if(piratewords.size()>0){
    Collections.shuffle(piratewords);
    System.out.println(piratewords.get(0));
}
Braj
  • 46,415
  • 5
  • 60
  • 76
  • How would I print out the random word in the console? – user3430741 Apr 13 '14 at 15:59
  • I have updated my post. just call `getRandom()` method and print the returned value. It will return `null` if the list is empty. – Braj Apr 13 '14 at 16:06
  • Sorry to keep bothering you, I am a beginner.`getRandom();{ System.out.println(piratewords.get(0)); }` I tried this but it deosn't work. I changed the String from the first line in your original post to Pirateword because it would compile otherwise. – user3430741 Apr 13 '14 at 16:22
  • all it prints is "Pirateword@"followed by random numbers and letters – user3430741 Apr 13 '14 at 17:24
  • Oh no. Its printing default `toString()` method implemention of `Object` class. Just override the `toString)` method of `Pirateword` class. or its object. print its instance fields value rather than object itself. – Braj Apr 13 '14 at 17:25
  • Read more about [How to use the toString method in Java?](http://stackoverflow.com/questions/3615721/how-to-use-the-tostring-method-in-java) – Braj Apr 13 '14 at 17:27