Results 1 to 5 of 5
  1. #1
    prairie_hoops is offline Member
    Join Date
    Oct 2009
    Posts
    2
    Rep Power
    0

    Default New here, pleas help!

    Hi, i'm really really new to Java and programming in general. I'm working my way through the Java for Dummies book and came across a problem with one of the examples. Here is the code:

    import java.util.Scanner;

    class VersatileSnitSoft {

    public static void main(String args[]) {
    Scanner myScanner = new Scanner(System.in);
    double amount;

    System.out.print("What's the price of a CD-ROM? ");
    amount = myScanner.nextDouble();
    amount = amount + 25.00;

    System.out.print("We will bill $");
    System.out.print(amount);
    System.out.println(" to your credit card.");
    }
    }


    Now, the program compiles but it crashes if i enter in a value with a decimal point.

    Here's the output after i enter a value with a decimal point.

    What's the price of a CD-ROM? 5.75
    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:840)
    at java.util.Scanner.next(Scanner.java:1461)
    at java.util.Scanner.nextDouble(Scanner.java:2387)
    at VersatileSnitSoft.main(VersatileSnitSoft.java:10)

    Process completed.

    Any help would be greatly appriciated. Thanks!

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Hi,

    you seem to have a Locale that formats Doubles with a comma as decimal seperator like a German Locale. If you input 5,75 it works.
    Alternatively make your scanner use a different Locale:
    Java Code:
    myScanner.useLocale(Locale.US);
    Last edited by PhHein; 10-28-2009 at 04:33 PM.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    prairie_hoops is offline Member
    Join Date
    Oct 2009
    Posts
    2
    Rep Power
    0

    Default It works!

    Thanks alot it worked with a comma! Although i tried:

    myScanner.useLocale(Locale.US);

    and it didn't work. Not to sure where that code should be
    inserted. But anyways it works, so thanks!

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

    Default

    Quote Originally Posted by prairie_hoops View Post
    Thanks alot it worked with a comma! Although i tried:

    myScanner.useLocale(Locale.US);

    and it didn't work. Not to sure where that code should be
    inserted. But anyways it works, so thanks!
    It didn't work but it did work? Cute.

    kind regards,

    Jos ;-)

  5. #5
    MarkWilson is offline Member
    Join Date
    Jun 2008
    Location
    us
    Posts
    47
    Rep Power
    0

    Default

    where i can get the all local code
    Java Code:
    useLocale(Locale.US);
    like this

Similar Threads

  1. Pleas help on finding errors
    By McXxT in forum New To Java
    Replies: 6
    Last Post: 04-02-2009, 08:42 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
  •