Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

JavaWorld Daily Brew

Please please help me!


Tags:

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.