Here is my code for my scanner:
import java.util.Scanner;
public class TryDouble {
public static void main(String [] args){
Scanner jin = new Scanner(System.in);
double a = jin.nextDouble();
double b = jin.nextDouble();
double c = jin.nextDouble();
System.out.println(a + b + c);
}
}
For input: 0.2 0.5 0.9, I got:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at TryDouble.main(TryDouble.java:6)
What can I do to remove that error?
It's a locale issue : If you want to use . you can try something like :
package test;
import java.util.Locale;
import java.util.Scanner;
public class TryDouble {
public static void main(String [] args){
Scanner jin = new Scanner(System.in).useLocale(Locale.US);
double a = jin.nextDouble();
double b = jin.nextDouble();
double c = jin.nextDouble();
System.out.println(a + b + c);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With