Results 1 to 7 of 7
  1. #1
    Mind Blown is offline Member
    Join Date
    Dec 2013
    Posts
    4
    Rep Power
    0

    Default 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:
    Java 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:
    Java 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?

    Thanks in advance.

    Additional Info:
    Operating System: Windows 8.1 Pro 64bit
    Java Version: 7.0.450.18 (64bit)
    Editor: Crimson Editor
    Last edited by Mind Blown; 12-04-2013 at 04:15 PM.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,345
    Blog Entries
    7
    Rep Power
    20

    Default Re: Inputting Decimal Value

    You did type an integer number for the item cost, did you?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,693
    Rep Power
    5

    Default 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.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    Mind Blown is offline Member
    Join Date
    Dec 2013
    Posts
    4
    Rep Power
    0

    Default 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.

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,809
    Rep Power
    19

    Default Re: Inputting Decimal Value

    Quote Originally Posted by gimbal2 View Post
    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.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,345
    Blog Entries
    7
    Rep Power
    20

    Default Re: Inputting Decimal Value

    Quote Originally Posted by gimbal2 View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,693
    Rep Power
    5

    Default Re: Inputting Decimal Value

    Alright, I learned something. I never use the Scanner class myself :)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Decimal to binary, octal to decimal
    By matejm1994 in forum New To Java
    Replies: 3
    Last Post: 12-26-2010, 09:59 AM
  2. Inputting some text in a GUI.
    By AJArmstron@aol.com in forum New To Java
    Replies: 6
    Last Post: 04-15-2010, 01:20 PM
  3. GUI's and inputting doubles or ints
    By lopder1 in forum New To Java
    Replies: 19
    Last Post: 11-05-2009, 08:50 PM
  4. Inputting custom numbers
    By Jakora33 in forum New To Java
    Replies: 14
    Last Post: 09-08-2009, 09:14 PM
  5. Reversing and inputting files
    By jigglywiggly in forum New To Java
    Replies: 14
    Last Post: 03-23-2009, 08:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •