# Inputting Decimal Value

• 12-04-2013, 04:49 PM
Mind Blown
Inputting Decimal Value
Was been searching around the net and I can't find a concrete answer or probably it is hard to understand those stuffs I saw.

So yeah, we have an activity and here is what I need to know;

- Write the appropriate Java statement to match the description in each of the following comments:
• //declare an integer variable for the cost of an item
• // declare a double variable for the sales tax percentage
• // prompt the user for the item cost
• // input the item cost
• // prompt the user for the sales tax percentage
• // input the sales tax percentage
• // output the amount of sales tax owed on the item, found by multiplying the item cost by the sales tax percentage

From what I've understand, here is what I made:
Code:

```import java.util.*; import java.io.*; import java.lang.*; class wew2{ public static void main(String [] args){         Scanner x = new Scanner(System.in);                         int cost;         final double stp;                 System.out.print("Enter Item Cost: ");         cost = x.nextInt();                 System.out.print("Enter Sales Tax Percentage: ");         stp = x.nextDouble();                 double sto = (cost * stp);                 System.out.println("Sales Tax Owed: "+sto);                         }}```
BUT... I'm having this error:
Code:

```Exception in thread "main" java.util.InputMismatchException         at java.utilScanner.throwFor(Unknown Source)         at java.utilScanner.next(Unknown Source)         at java.utilScanner.nextDouble(Unknown Source)         at wew2.main(wew2.java:16)```
I've been researching everywhere on how can I input a decimal value in the STP since it is asking for the tax percentage, since it is asking for the tax percentage... you have to convert the percentage into (EX. 10% = *0.10).

How to do this?

Operating System: Windows 8.1 Pro 64bit
Java Version: 7.0.450.18 (64bit)
Editor: Crimson Editor
• 12-04-2013, 05:20 PM
JosAH
Re: Inputting Decimal Value
You did type an integer number for the item cost, did you?

kind regards,

Jos
• 12-04-2013, 05:24 PM
gimbal2
Re: Inputting Decimal Value
Its complaining about the nextDouble(), so on the input of the tax. Looks like this is the good old "newline still in the character stream" problem that passed by at least a few times a week.
• 12-04-2013, 05:30 PM
Mind Blown
Re: Inputting Decimal Value
Ok, I found out the solution.

I just found out that the system language/region of my Windows is Swedish/Sweden, wherein our decimal is komma (,) not period (.) (my tutor has an English US region Windows).
It was a huge fail of me not figuring it out and not realizing it the first place after trying and trying again and again in 3 whole hrs.

Right now, all I did was... changed my system language/region to English (US).

Posting the solution for those who don't know.
Anyways, thanks guys and sorry for my stupidity.
• 12-04-2013, 05:47 PM
Tolls
Re: Inputting Decimal Value
Quote:

Originally Posted by gimbal2
Its complaining about the nextDouble(), so on the input of the tax. Looks like this is the good old "newline still in the character stream" problem that passed by at least a few times a week.

It wouldn't be (well, it isn't based on the follow up post).
That only occurs when you mix in nextLine() calls with the other nextXXX() calls.
nextLine() is the odd one out, in that it doesn't discard a leading token in the buffer, it reads up to a token.
• 12-04-2013, 05:48 PM
JosAH
Re: Inputting Decimal Value
Quote:

Originally Posted by gimbal2
Its complaining about the nextDouble(), so on the input of the tax. Looks like this is the good old "newline still in the character stream" problem that passed by at least a few times a week.

No it isn't: new line characters are considered white space; I ran the code and it ran fine ...

kind regards,

Jos
• 12-04-2013, 05:53 PM
gimbal2
Re: Inputting Decimal Value
Alright, I learned something. I never use the Scanner class myself :)