Results 1 to 8 of 8
  1. #1
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Exclamation Double not taking a point (eg 10.12)

    Hi,

    I am trying a simple new program, but it does not take the decimal point :(

    This is my program:
    Java Code:
    import java.util.Scanner; 
    
    public class apples3 {
    	public static void main(String args[]){
    
    		Scanner bucky = new Scanner(System.in);
    		double first_no, second_no, answer;
    		
    		System.out.println("Enter the first number:");
    		first_no=bucky.nextDouble();
    		System.out.println("Enter the Second number:");
    		second_no=bucky.nextDouble();
    
    		answer = first_no + second_no; 
    		System.out.println(answer);
    	}
    }
    if I use a decimal point I get this error:
    Java Code:
    Enter the first number:
    56.55
    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 apples3.main(apples3.java:10)
    but if I use a comma, then it runs as expected.

    Eg:
    10.12 (does not work)
    10,12 (works)

    is it because I am on a Swedish system (we use commas here as decimal points eg: 100,12.00) or something else?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by N00Bie View Post
    is it because I am on a Swedish system (we use commas here as decimal points eg: 100,12.00) or something else?
    Yes absolutely, and this is because the parser is using the Swedish Locale. To use a different system, you'll need to set the Locale. I'm not sure if this can be used with Double.parseDouble (if anyone knows, please educate me!), but I know it can be used with NumberFormat instances.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    is it because I am on a Swedish system (we use commas here as decimal points eg: 100,12.00)

    That's it exactly. Scanner reads the input string and attempts to make sense of it as the sort of string you use to specify floating point values in Sweden. Which is reasonable behaviour if you think about it.

    This behavour - and what you do about it if you don't want the scanner to detect Swedish numerals for whatever reason - is described in the Scanner API docs. Look for the section "Localized numbers" and post back if you get stuck.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    @Fubarable: I'm lazy (like most English speakers) so I won't test Double.parseDouble()... But the API docs for Double.doubleValue() tell you to use NumberFormat instead.

    The OP's nextDouble() should be good.

  5. #5
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    Thanks guys!

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You're welcome.

  7. #7
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    Totally off topic:
    I think I am going to like it here, you guys are friendly and helpful, at first I was a bit scared if my questions were too much of a newb and thus wont get answered or I would be laughed at but I have gotten nothing but friendly help!

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Thanks.

    I also like the tone here - and the fact that it is maintained by the community itself with few (any?) appeals to rules or attempts to enforce "niceness".

    Special thanks are due to the labour of Fubarable and the other moderators for their work in taking out the spam garbage etc.

Similar Threads

  1. double a * double b = weird output
    By GPB in forum New To Java
    Replies: 3
    Last Post: 03-26-2010, 10:40 AM
  2. Double.valueOf() vs Double.parseDouble()
    By greenbean in forum New To Java
    Replies: 10
    Last Post: 01-12-2009, 08:39 AM
  3. Taking passwords on the console
    By eva in forum Advanced Java
    Replies: 2
    Last Post: 12-19-2007, 09:28 AM
  4. Taking input from console
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-05-2007, 04:47 PM

Tags for this Thread

Posting Permissions

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