tags:

views:

58

answers:

4

This code is works fine when you type 22 and 56 for example, it shows you the correct result

  import java.util.Scanner;

class apples{
    public static void main(String args[]){
    Scanner villy = new Scanner (System.in);
    double fnum, snum, answer;
    System.out.println("Enter first num: ");
    fnum = villy.nextDouble();
    System.out.println("Enter second num: ");
    snum = villy.nextDouble();
    answer = fnum + snum;
    System.out.println(answer);
    }
}

but when you try to put decimals for example 23.53 and 42.76 it fails with the following exception (see screenshot):

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 apples.main(apples.java:8)
A: 

The following code snippet works fine. Your problem must lie elsewhere. What do you mean by "it fails"? Does it give you an error message or exception?

double fnum=0, snum=0, answer = 0;
fnum=23.53;
snum=42.76;
answer=fnum+snum;
System.out.println("answer="+answer); //prints 66.28999999999999
CoolBeans
Yes he gives me a error, see the link with the screenshot
There must be something I am not getting. When I run your code it works fine. See my output from your code below:-Enter first num: 23.53Enter second num: 42.7666.28999999999999
CoolBeans
I put this nº and gives me the same error .. something strange is happening with Eclipse in my pc ., a few hours ago i opened another topic that i have problems with some specific codes
Can you add a check for if (villy.hasNextDouble())? Like this:-if(villy.hasNextDouble()){ fnum = villy.nextDouble();}
CoolBeans
sorry but i do not understand ., what lines of code i need to change?
+1  A: 

Take a look at the API description of your exception.

Class InputMismatchException:

Thrown by a Scanner to indicate that the token retrieved does not match the pattern for the expected type, or that the token is out of range for the expected type

You're likely not feeding in the input you expect; at any rate, the scanner's "next" item at some point is not a double.

Looking at your screenshot, you appear to be using the API correctly. I suspect that Eclipse's built-in console support isn't doing quite what the scanner expects. Try changing the first line of your method to:

Scanner villy = new Scanner("23.53\n42.76\n");

This should be functionally identical to what you would get from the input stream; if this works then it suggests the problem is with Eclipse's input.

One thing to try, which is always a good idea in general, is to explicitly specify the encoding of the input stream. What you want to send is characters, but what you're actually sending is bytes. If Eclipse uses some unusual character encoding, and the Scanner guesses a different one by default, you're going to run into problems. Specifying the actual encoding in the scanner constructor will remove this possibility.

Another option is that the Locale that Java is running with actually uses the comma as a decimal separator. If this is the case then the Scanner is right, you aren't specifying a double correctly (though this should fail on the String-based example too).

Andrzej Doyle
x10 Andrzej, thanks
+1  A: 

Are you on a non-english computer by any chance ? With a french or german locale, for instance, you have to enter 23,53, not 23.53.

The default locale for the Scanner is what Locale.getDefault() returns: on an english computer, it should return Locale.ENGLISH. You can set the locale to english by doing this:

Scanner villy = new Scanner(System.in).useLocale(Locale.ENGLISH);
Samuel_xL
That's a good point.
CoolBeans
it works, thanks Samuel
+1  A: 

You likely typed a comma instead of a period when inputting the numbers. That would throw an InputMismatchException. I've run the code and it works fine with properly typed input, but throws InputMismatchException when I replaced the period with any other character.

Anita