I am fairly new to Java and getting user input is proving to be difficult and the teacher didn't really explain it very well, and all resources I used didn't help with my problem:
I am trying to write a simple program that continues to read user input and stores it in a Queue (-> what the program is actually about) until a certain String is entered. First it takes two Strings and then two doubles. The first time everything works fine but with the second iteration for some reason it skips the second String input and goes straight to the first double input.
Here is an example code:
System.out.print("Enter first string: ");
while(!line.equals("-") && scanner.hasNext())
{
line = scanner.nextLine();
if(!line.equals("-"))
{
a.offer(line);
System.out.print("Enter second string: ");
if(scanner.hasNextLine())
{
line = scanner.nextLine();
b.offer(line);
}
System.out.print("Enter first double: ");
double tmp = scanner.nextDouble();
c.offer(tmp);
System.out.print("Enter second double: ");
tmp = scanner.nextDouble();
d.offer(tmp);
}
System.out.print("Enter first string: ");
}
I have only programmed in C up until now and getting user input was way easier there. So what am I doing wrong?
EDIT: It was marked as duplicate of Skipping nextLine() after using next(), nextInt() or other nextFoo() methods I looked at it and in that question the problem was that the newline character at the end wasn't consumed due to the use of .nextInt(). But I am using a .nextLine() which should consume the newline, so I still don't get why it's happening.