Reputation: 1223
Here is an example from Bruce Eckel Thinking in Java, 4th edition
import java.io.BufferedReader;
import java.io.StringReader;
import java.util.Scanner;
public class BetterRead {
public static void main(String[] args) {
BufferedReader input = new BufferedReader(new StringReader("Sir Robin of Camelot\n22 1.61803"));
Scanner stdin = new Scanner(input);
System.out.println("What is your name?");
String name = stdin.nextLine();
System.out.println(name);
System.out.println("How old are you? What is your favorite double?");
System.out.println("(input: <age> <double>)");
int age = stdin.nextInt();
double favorite = stdin.nextDouble();
System.out.println(age);
System.out.println(favorite);
System.out.format("Hi %s.\n", name);
System.out.format("In 5 years you will be %d.\n",
age + 5);
System.out.format("My favorite double is %f.",
favorite / 2);
}
}
here is output
What is your name?
Sir Robin of Camelot
How old are you? What is your favorite double?
(input: <age> <double>)
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextFloat(Scanner.java:2388)
at betterread.BetterRead.main(BetterRead.java:21)
Java Result: 1
I'm pretty new to Java, but as you can see in this example int follows buy double. So it might work right. But still throws InputMismatchException. How to solve this problem?
Upvotes: 0
Views: 1526
Reputation: 1
I think the line problem may be with Scanner stdin = new Scanner(input);
, here instead try this: Scanner stdin = new Scanner(System.in);
Upvotes: 0
Reputation: 2029
The problem seems to be your Locale, as 1.61803 doesn't seem to be a valid double for it. Try with 1,61803.
Upvotes: 1