Results 1 to 5 of 5
Thread: New here, pleas help!
- 10-28-2009, 04:27 PM #1
Member
- Join Date
- Oct 2009
- Posts
- 2
- Rep Power
- 0
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!
- 10-28-2009, 04:30 PM #2
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
- 10-28-2009, 04:40 PM #3
Member
- Join Date
- Oct 2009
- Posts
- 2
- Rep Power
- 0
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!
- 10-28-2009, 05:02 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,392
- Blog Entries
- 7
- Rep Power
- 17
- 10-29-2009, 07:33 AM #5
Member
- Join Date
- Jun 2008
- Location
- us
- Posts
- 47
- Rep Power
- 0
Similar Threads
-
Pleas help on finding errors
By McXxT in forum New To JavaReplies: 6Last Post: 04-02-2009, 08:42 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks