0

I am getting an error message from the output when I typed : 199.99

 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.nextFloat(Scanner.java:2388)
    at myproject2.Myproject2.main(Myproject2.java:11)

but my code doesn't have any errors:

package myproject2;

import java.util.Scanner;
public class Myproject2 {


    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    System.out.print("Enter purchase amount: ");
    double purchaseAmount = input.nextDouble();

    double tax = purchaseAmount * 0.06;
    System.out.println("Sales tax is " + (int)(tax * 100) / 100.0);


    }

}

Please help me!

6
  • 2
    Hmm. The stack trace shows nextFloat, but your code shows nextDouble. Are you sure that stack trace was obtained by running that code? Commented Jan 30, 2014 at 17:18
  • Did you give input other than a number? Commented Jan 30, 2014 at 17:18
  • And what locale are you running in? I wonder whether it was expecting "199,99". Commented Jan 30, 2014 at 17:19
  • @Vamshi : No I'm sure I give a number and I'm getting this error for any number but integer. Commented Jan 30, 2014 at 17:39
  • 2
    If you solved your problem, don't just add [Solved] to your title. Perhaps tell us how you solved it in an answer. Or if one of the answers solved it for you, accept the answer. Commented Jan 30, 2014 at 18:48

2 Answers 2

1

The nextDouble method expects the input to be like A,B. If your input is 199,99, there will be no errors.

If you want it to accept doubles the way you said, you can set the Locale to your Scanner.

input.useLocale(Locale.US);
Sign up to request clarification or add additional context in comments.

1 Comment

the expected input depends on the current locale
0
input.useLocale(Locale.UK);

You need to provide the Scanner's locale.

E.g.

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

2 Comments

I'm newbie in java and I don't know how to do this.
I've edited my answer to provide you with an example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.