Page 1 of 2 12 LastLast
Results 1 to 20 of 28
  1. #1
    Freakzoyd is offline Member
    Join Date
    Jul 2010
    Posts
    27
    Rep Power
    0

    Default Cant use doubles

    Hey guys, I was making a simple program to make a calculator and I suppose the code is alright because I just copy it from the book. The problem is that I stated the variables as doubles and every time I input a decimal point number, an error message is shown, What is wrong with it? Thx in advance

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

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    What's the full error message with stack trace, and point out the line the stack trace is referring to in your code?

  3. #3
    Sno's Avatar
    Sno
    Sno is offline Senior Member
    Join Date
    Apr 2010
    Posts
    196
    Rep Power
    5

    Default

    Works perfectly fine for me!

    output:

    Enter first num:
    1
    Enter second num:
    2
    3.0
    :rolleyes: ~ Sno ~ :rolleyes:
    '-~ B.S. Computer Science ~-'

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Seems to me too, there is no any error in your code. Can you post the complete error message if so?

  5. #5
    Freakzoyd is offline Member
    Join Date
    Jul 2010
    Posts
    27
    Rep Power
    0

    Default

    It also works for me if I put integers, but if i put 1.2 or any number with decimal in the first number, it will tell me this:

    PHP Code:
    Enter first num: 
    1.2
    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)

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default

    Check your locale. What character is used for "decimal point".
    Americans use the .
    European use the ,

    Try entering: 1,2

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  8. #8
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    Ah!
    I think Norm has it...

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  10. #10
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    Well, no...unless that's the locale specified on the OPs machine.
    Then that is the problem...not Java.

    A number of European locales take a comma as the decimal separator.

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  12. #12
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    I'm not sure what you're getting at.

    It is quite possible that whatever machine the OP is running this code on has its locale set to something that treats a comma as the decimal separator. This is the locale that Java will pick up on, and so is the format the Scanner will expect for a double.

    If that's the case then the OP has two options, use "1,2" as their input, or set the locale that the Scanner should use in the code.

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    This is the locale that Java will pick up on, and so is the format the Scanner will expect for a double.
    So what I'm saying is avoid it as much as possible. Hope it's clear to you. :)

  14. #14
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    The thing is, though, it is standard.

    This is slightly apropos to what I'm doing at the moment, in that we get uploads for data from parts of continental Europe that need to be checked for locale because some of it uses commas. And there it is the norm...

  15. #15
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    That is weird I myself am from Europe and decimals work as you would expect, on a side note the OP is learning from bucky on youtube which is worth checking out for alot of begginners very good basic tutorials on java.

  16. #16
    Freakzoyd is offline Member
    Join Date
    Jul 2010
    Posts
    27
    Rep Power
    0

    Default

    Thanks to all, I am able able to run this program with what Norm said. Thx Norm. I will give rep to all if i'm allowed. Yeah lol, i am looking at bucky's videos on youtube to learn java. Do this tutorial teach different from others?
    Last edited by Freakzoyd; 07-22-2010 at 12:36 AM.

  17. #17
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    just out of interest what country are you from?

  18. #18
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    The thing is, though, it is standard.

    This is slightly apropos to what I'm doing at the moment, in that we get uploads for data from parts of continental Europe that need to be checked for locale because some of it uses commas. And there it is the norm...
    Hmmm, worth to look around. Let me check.

  19. #19
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    Switzerland...at least the bods in Switzerland I seem to be currently dealing with.

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default

    Did you read the posts on this thread to see the solution?

Page 1 of 2 12 LastLast

Similar Threads

  1. arrays using doubles [SOLVED]
    By soccer_kid_6 in forum New To Java
    Replies: 0
    Last Post: 03-10-2010, 01:56 AM
  2. GUI's and inputting doubles or ints
    By lopder1 in forum New To Java
    Replies: 19
    Last Post: 11-05-2009, 08:50 PM
  3. Problem with division using doubles
    By chrismanahan in forum New To Java
    Replies: 3
    Last Post: 10-10-2009, 09:26 PM
  4. arrays strings and doubles
    By rgvbabe in forum New To Java
    Replies: 1
    Last Post: 01-13-2008, 11:26 PM

Posting Permissions

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