Results 1 to 5 of 5
  1. #1
    top3player is offline Member
    Join Date
    Oct 2012
    Posts
    1
    Rep Power
    0

    Default Most simple program issue

    Hello,

    I am doing a first small practice assigment in Java.

    The code is the following:

    Java Code:
    import java.util.Scanner;
    
    class Speed {
    
        public static void main(String[] args){
            Scanner sc = new Scanner(System.in);
            System.out.print("Speed in MPH): ");
    	double mih = sc.nextDouble();
    	double kmh = (mih * 1.609344);
    
    	System.out.println("Speed in kmh: " + kmh);
    
        }
    
    }
    First of all my phrasing is probably horrendous but I believe I can be understood...
    When launching the program when entering a value with decimals using " , " as separation. For example 40,5, everything works out fine.

    However when using a " . " making it 40.5 I get the following errors

    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Sourche>
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextDouble(Unknown Source)
    at Hastighet.main(Hastighet.java:8)

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

    Default Re: Most simple program issue

    Try using sc.useLocale() so that the scanner uses a locale where the symbol for a decimal point is "dot" rather than "comma". The full details of what goes on as the scanner reads and parses the input stream to obtain a double value to return is described in that documentation.

    The Locale class provides constants whose values are the locales appropriate to various countries.

  3. #3
    gowthamgutha is offline Member
    Join Date
    Sep 2012
    Location
    Guntur, India
    Posts
    27
    Rep Power
    0

    Default Re: Most simple program issue

    Hastighet.main(Hastighet.java:8)
    what is this first of all, Where is this class Hastighet.java, this might be another class. You may be executing another class not this one. The code looks fine.

    [Moderator edit: blog spam removed]
    Last edited by DarrylBurke; 10-04-2012 at 04:45 PM.

  4. #4
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default Re: Most simple program issue

    If I write in Eclipse I alsways get a message that the Eclipse would like me to put Scanner in a try catch.
    Maybe that would be usefull too?

  5. #5
    Games2Design is offline Member
    Join Date
    Sep 2012
    Posts
    44
    Rep Power
    0

    Default Re: Most simple program issue

    Quote Originally Posted by Lund01 View Post
    If I write in Eclipse I alsways get a message that the Eclipse would like me to put Scanner in a try catch.
    Maybe that would be usefull too?
    That is not necessary. You need to do that when your writing to or reading from a file.

Similar Threads

  1. Simple Scanner Issue
    By jazzermonty in forum New To Java
    Replies: 7
    Last Post: 07-17-2012, 11:47 PM
  2. Replies: 7
    Last Post: 11-26-2011, 12:03 PM
  3. Conceptual Issue? Please Help, should be simple.
    By justinm231 in forum New To Java
    Replies: 1
    Last Post: 11-18-2011, 11:03 PM
  4. Simple program, simple problem
    By taymilll in forum New To Java
    Replies: 12
    Last Post: 06-20-2011, 06:12 AM
  5. Simple Inheritance issue...
    By AWE in forum New To Java
    Replies: 3
    Last Post: 07-27-2009, 10:56 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
  •