I'm rather new to java and I am playing around with scanners. This is my code:
System.out.print("Name? ");
String name = s.nextLine();
System.out.print("Height? ");
double height = s.nextDouble();
System.out.print("Haircolor? ");
String haircolor = s.nextLine();
System.out.print("Age? ");
int age = s.nextInt();
System.out.print("Job? ");
String job = s.nextLine();
As you can probably see, I'm getting input about some person. Now the problem is that it gives an InoutMismatchException
when I try to read the double. After reading this thread I changed that line to double height = Double.parseDouble(s.nextLine());
. That works but now it doesn't stop after printing "Job? ". The program just goes on without reading anything.
Can anyone maybe tell me why that is? And also why I got this exception? I just suspected that a scanner maybe can't read different types (double, string, int) and can't "switch back" to String
after having read an int
. But that seems kinda odd.
Thank you so much in advance for an answer.
Tony