nextDouble() throws an InputMismatchException when I enter a double
It might be locale dependent. Decimal numbers are e.g written as 0,5 in Sweden.
Change your code so that it says e.g.:
Scanner scan = new Scanner(System.in);
scan.useLocale(Locale.US);
Scanner#nextDouble fails to parse 5.2 (InputMismatchException)
Scanner and Locale
It is not directly obvious from the Javadoc of Scanner#nextDouble
but it is Locale-sensitive.
There are various things that influence the way how Scanner
expects numbers and text to look like that depend on your current set Locale
.
By default, Scanner
uses your current systems locale (country and language settings). So you have to enter the numbers in the way they are usually written in your current region.
Decimal separator . vs ,
In your particular case, it is the decimal separator which you entered with .
(dot) but Scanner
expected a ,
(comma) instead. This is for example the case with a Moldovan locale, since in Moldovan comma is used instead.
Hence, when you entered 5.2
it did not see any double
, as specified by your current Locale
. But when you enter 5,2
, it works.
You can read more about this in the Javadoc of Scanner
, section Localized numbers. Also see this illustration from Wikipedia:
Change locale
That said, you can use Scanner#useLocale
to change the locale Scanner
uses. For example:
scanner.useLocale(Locale.ENGLISH);
and then you can also work with 5.2
as input.
Scanner double value - InputMismatchException
You should precise a Locale for your Scanner.
Scanner scanner = new Scanner(System.in).useLocale(Locale.US);
From the doc :
An instance of this class is capable of scanning numbers in the
standard formats as well as in the formats of the scanner's locale. A
scanner's initial locale is the value returned by the
Locale.getDefault() method; it may be changed via the
useLocale(java.util.Locale) method
The localized formats are defined in terms of the following
parameters, which for a particular locale are taken from that locale's
DecimalFormat object, df, and its and DecimalFormatSymbols object,
dfs.
So your default locale use certainly a DecimalFormat that expect a comma as a decimal delimiter instead of a dot.
Error when using input.nextDouble in Java
I tested your code on an online IDE and it worked, then I tested on my machine and got the same error as you, then I tested again, but using commas (,) instead of dots(.) for the decimal point, and it worked. The InputMismatchException is throw because the Scanner object uses the default Locale of the JVM to parse the input value, and when it is different from the expected it causes the exception.
You can pass a locale to the scanner that uses . as a decimal separator:
input.useLocale(Locale.US);
or just use (,) when entering your values.
Also, there is another error in your code, in the last printf:
System.out.printf("%nBMI = %d", BMI);
You should use a %f for floating points instead of a %d, that is used for integers. You can find more about how to format numeric output in the following documentation.
nextDouble() don't accept points, just commas
Your default locale uses the comma character (,
) as the decimal separator, and thus fails parsing inputs with decimal points (.
) as doubles. You can override this behavior by explicitly setting a Locale
that accepts decimal points. E.g.:
Scanner scan = new Scanner(System.in);
scan.useLocale(Locale.US);
How do I catch InputMismatchException for doubles a, b, and c in this code?
public static void main(String[] args) {
Scanner input = new Scanner (System.in); //scanner
short repeat = 1;
while (repeat == 1) {
System.out.println("Enter your equation by entering a, b, and c."); //introduction
System.out.println("Press enter evey time you enter a number.");
try {
double a = Double.parseDouble(input.nextLine());
double b = Double.parseDouble(input.nextLine());
double c = Double.parseDouble(input.nextLine());
double answer1 = ( (-b) + Math.sqrt( Math.pow(b, 2) - (4*a*c) ) ) / (2*a); //answers
double answer2 = ( (-b) - Math.sqrt( Math.pow(b, 2) - (4*a*c) ) ) / (2*a);
System.out.println("Your answers are: " + answer1 + " and " + answer2);
}
catch (NumberFormatException e) {
System.out.println("That's not a valid number.");
}
System.out.println("Would you like to calculate more numbers? 1 for yes, 0 for no: ");
repeat = Short.parseShort(input.nextLine());
}
input.close();
}
You have initalize the repeat variable to 1 and not to 0, since the program will not enter the while loop otherways.
You need to put your calculations for answer1 and answer2 in the try-catch-block too, because if the user enters an invalid value, the result shouldn't get calculated.
You should use only one Scanner instance, so you mustn't instance it in the loop.
Related Topics
How I Save and Retrieve an Image on My Server in a Java Webapp
How to Return JSON Data from Spring Controller Using @Responsebody
Counting the Number of Files in a Directory Using Java
Java Change the Document in Documentlistener
Difference Between Double and Double in Comparison
Cors Allowed-Origin Restrictions Aren't Causing the Server to Reject Requests
Build Project into a Jar Automatically in Eclipse
Filter Java Stream to 1 and Only 1 Element
Converting String to "Character" Array in Java
Jtable Row Hightlighter Based on Value from Tablecell
What Is the "Default" Implementation of Method Defined in an Interface
How to Stop a Scheduled Task That Was Started Using @Scheduled Annotation
Java Jtree Directory Structure from File Paths
Deciphering Variable Information While Debugging Java
How to Encrypt or Decrypt with Rijndael and a Block-Size of 256 Bits