Reputation: 1
I am trying to prompt user to enter x coordinate but when I write a decimal number such as 2.1 as an input this is caused a problem .How can I fix the problem?
import java.util.Scanner;
public class TwoRetangles{
public static void main(String[]args){
Scanner input=new Scanner(System.in);
System.out.print("Enter the center x coordinate of retangle = ");
double x1=input.nextDouble();
}
}
--------------------Configuration: --------------------
Enter the center x coordinate of retangle = 2.1
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.nextDouble(Scanner.java:2456)
at TwoRetangles.main(TwoRetangles.java:6)
Process completed.
Upvotes: 0
Views: 1459
Reputation: 407
Your code works for me.. Scanner is tied to your system settings I believe, so if its not for US you couldn't use a decimal like 2.1.
If that is the case, do
Scanner input = new Scanner(System.in).useLocale(Locale.US);
You would need to import java.util.*;
Upvotes: 4
Reputation: 62
There is no error.It is working fine in my system.Make sure you compiled it right.It is accepting the value.I tried this code and it is printing 2.1:
import java.util.Scanner;
public class TwoRetangles{
public static void main(String[]args){
Scanner input=new Scanner(System.in);
System.out.print("Enter the center x coordinate of retangle = ");
double x1=input.nextDouble();
System.out.print(x1);
}
}
Upvotes: 0