Scanner Double Value - Inputmismatchexception

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.

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.

reading double from a file using scanner, java.util.InputMismatchException

When reading from file I would recommend using getNextLine(), it makes for much simpler code and extraction of values. Plus your data file you read in is colon separated which makes for extraction of values quite easy. try using this while loop instead

while(ReadFile.hasNextLine())
{
String inputOfFile = ReadFile.nextLine();
String[] info = inputOfFile.split(":");
try{
if(info.length == 3)
{
val = Integer.parseInt(info[0]);
price = Double.parseDouble(info[1]);
quant = Integer.parseInt(info[2]);
stores[i].LojaValor(val);
stores[i].LojaQuantidade(quant);
stores[i].LojaPreco(price);
i++;
}
else
{
System.err.println("Input incorrectly formatted: " + inputOfFile);
}
}catch (NumberFormatException e) {
System.err.println("Error when trying to parse: " + inputOfFile);
}
}

My guess is there is an extra new line character or something inside the file you are reading from. The above should be able to handle your file quite easily. Another plus to this implementation is that it will continue to read from the file even if you run into places in the file where the data is incorrectly formatted.

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:

Countries using decimal comma

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.

Attempting to read double values from a textfile, but a InputMismatchException is thrown

Most likely your scanner uses the wrong locale. Based on the values of your Salary.txt, you simply have to change your scanner's locale to english. Try this:

import java.util.Locale;
...
Scanner input = new Scanner(url.openStream());
input.useLocale(Locale.ENGLISH);


Related Topics



Leave a reply



Submit