I am accepting multiple lines of info (strings until I parse them later). For example:
1 5 0
2 9 6
2 9 1
I wrote this code to separate the lines because I'm going to have to manipulate each line in a way.
Scanner scan = new Scanner(System.in);
System.out.println("Enter input: ");
ArrayList<String> lines = new ArrayList<String>();
while(scan.hasNextLine()) {
lines.add(scan.nextLine());
}
System.out.println(lines.get(0));
And I tested this, it does indeed seperate each line of my input and add it into my arraylist, however the part inside the while loop that says scan.nextLine() is checking for more input, causing an infinite loop.
How do i fix this?
hasNextLine()isn't capable of reading your mind. You have to decide how to communicate to the program that you don't want it to wait for more input. You might want to go with a predetermined number of lines, or decide on some special input that indicates you're done.