-1

How do I use tryparce in Java to solve the problems below? Code exampe is helpful.

enter image description here

enter image description here

import java.util.Scanner;

class BasicCalculator {
    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);
        double fnum, snum, answer; // firstnumber, 2nd number, answer
        System.out.println("Enter first number: ");
        fnum = input.nextDouble();
        System.out.println("Enter 2nd number: ");
        snum = input.nextDouble();

        answer = fnum + snum;
        System.out.println(answer);
    }
}

Enter first number: 
32.5
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 basicCalculator.BasicCalculator.main(BasicCalculator.java:8)
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 1
    That code should work. – Hovercraft Full Of Eels Feb 15 '17 at 15:22
  • Try re-compiling and running it again. – Hovercraft Full Of Eels Feb 15 '17 at 15:23
  • Possibly related: [Input Mismatch Exception](http://stackoverflow.com/q/14027537/4204026) – Drew Kennedy Feb 15 '17 at 15:24
  • 3
    @HovercraftFullOfEels Judging by the clock in the users Screenshot he is probably sitting in central europe and has his Locale set to german/spanish/french or something similar and therefor has to enter the number with a comma. user3909365: Try "32,5" as input. – OH GOD SPIDERS Feb 15 '17 at 15:27
  • What is tryparce and what is the image for? – Axel Feb 15 '17 at 15:31
  • @911DidBush: **WOW**! Very good eyes!!! – Hovercraft Full Of Eels Feb 15 '17 at 15:33
  • From the docs: “An instance of (`Scanner`) is capable of scanning numbers in the standard formats as well as in the formats of the scanner's locale.” Wouldn’t this mean that `32.5` should work? – Ole V.V. Feb 15 '17 at 15:38
  • 1
    @OleV.V. Just tried it myself (located in Germany myself) and I also get a InputMismatchException when entering numbers with point as decimal mark. The inconsistency that java classes have when it comes to that is actually quite annoying. Some classes use the locale (like scanner seems to do), others like the String constructor of BigDecimal only support point. Really wish java would pick one way and stick to it. :-/ – OH GOD SPIDERS Feb 15 '17 at 15:46
  • In that case you can probably solve the problem by issuing `input.useLocale(Locale.ROOT)` before trying to read the doubles. – Ole V.V. Feb 15 '17 at 15:59
  • There is no tryparce or tryparse in the standard Java libraries (checked the index for Java 8 SE). – Ole V.V. Feb 15 '17 at 16:01

1 Answers1

1

TL;DR: useLocale(Locale.ROOT).

I ran your program and entered 32.5 and 1,000, no error. I then changed my computer’s general format to German and ran again. This time I saw the same stacktrace as in your question. Next I changed the initialization of the Scanner to:

    Scanner input = new Scanner(System.in).useLocale(Locale.ROOT);

Now your program again works as I think you expected:

Enter first number: 
32.5
Enter 2nd number: 
1,000
1032.5
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161