I need to copy 10 messages from my .txt file using the scanner. I then need to put them into an array, so that later I can I can print a message that has a chance of being any of the 10.
I have tried a few different things, but I do not know much in this area.. So far I have
Code
public static void main(String[] args) {
try {
BufferedReader reader = new BufferedReader(new FileReader(new File("winningmessages.txt")));
List list = new ArrayList();
String line = reader.readLine();
while(line != null){
list.add(line);
System.out.println(line);
line = reader.readLine();
}
reader.close();
String[]records = (String[])list.toArray(new String[]{""});
} catch(Exception ex){
System.out.println(ex);
}
The println prints all of the lines at once.. I am trying to store them into an array so that later I can call on the array to display 1 random message.
I was given a hint it should look something like this:
System.out.println(winningMessages[myRand.nextInt(10)]);
Was wondering if anyone could explain how to store this info from text files into an array.. I will have to do this again for losing messages.