I'm trying to get Scanner to read input in the following format:
0, 0
1, 1
2, 2
3, 3
And putting each pair into a queue. However, It's skipping over the first and second to last line. this is my code:
Scanner input = new Scanner(System.in);
System.out.println("Enter values: ");
while (input.hasNextLine() && !input.nextLine().equals("-1")) {
System.out.println(jobs.peek());
String str = input.nextLine();
int aT = Integer.parseInt(String.valueOf(str.charAt(0))); // this is the first number
int sT = Integer.parseInt(String.valueOf(str.charAt(3))); // this is the second number after the space
}
input.close();
I know the problem has to do with the !input.nextLine().equals("-1")
part of my while loop condition since it doesn't skip anything when I remove it. But if I do that I get an index out of bound exception when it tries to read the last empty line (which is why I'm trying to use -1 as a sentinel). What am I doing wrong?