Most simple program issue
Hello,
I am doing a first small practice assigment in Java.
The code is the following:
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
Quote:
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)
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.
Re: Most simple program issue
Quote:
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]
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?
Re: Most simple program issue
Quote:
Originally Posted by
Lund01
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.