Results 1 to 5 of 5
Thread: Most simple program issue
- 10-03-2012, 09:16 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 1
- Rep Power
- 0
Most simple program issue
Hello,
I am doing a first small practice assigment in Java.
The code is the following:
First of all my phrasing is probably horrendous but I believe I can be understood...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); } }
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)
- 10-03-2012, 09:57 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 10-04-2012, 01:18 PM #3
Member
- Join Date
- Sep 2012
- Location
- Guntur, India
- Posts
- 27
- Rep Power
- 0
Re: Most simple program issue
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.Hastighet.main(Hastighet.java:8)
[Moderator edit: blog spam removed]Last edited by DarrylBurke; 10-04-2012 at 03:45 PM.
- 10-04-2012, 02:22 PM #4
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?
- 10-04-2012, 03:44 PM #5
Member
- Join Date
- Sep 2012
- Posts
- 44
- Rep Power
- 0
Similar Threads
-
Simple Scanner Issue
By jazzermonty in forum New To JavaReplies: 7Last Post: 07-17-2012, 10:47 PM -
Simple client/server application with FileOutputStream issue
By andrearro88 in forum NetworkingReplies: 7Last Post: 11-26-2011, 11:03 AM -
Conceptual Issue? Please Help, should be simple.
By justinm231 in forum New To JavaReplies: 1Last Post: 11-18-2011, 10:03 PM -
Simple program, simple problem
By taymilll in forum New To JavaReplies: 12Last Post: 06-20-2011, 05:12 AM -
Simple Inheritance issue...
By AWE in forum New To JavaReplies: 3Last Post: 07-27-2009, 09:56 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks