Running the following in Eclipse
initially caused Scanner to not recognize carriage returns in the console effectively
blocking further input:
price =
sc.nextFloat();
Adding
this line before the code causes Scanner to accept 0,23 (french notation) as a
float:
Locale.setDefault(Locale.US);
This
is most probably due to regional settings in Windows XP Pro (French/Belgian). When the
code is run again 0,23 is still accepted and entering 0.23 causes it to throw a
java.util.InputMismatchException
.
Any
explanation as to why this is happening? Also is there a workaround or should I just use
Float#parseFloat
?
Edit:
This demonstrates how Scanner behaves with different Locales (uncomment one of the lines
at the
beginning).
import
java.util.Locale;
import
java.util.Scanner;
public class NexFloatTest
{
public static void main(String[] args) {
//Locale.setDefault(Locale.US);
//Locale.setDefault(Locale.FRANCE);
// Gives fr_BE on
this system
System.out.println(Locale.getDefault());
float price;
String uSDecimal = "0.23";
String
frenchDecimal = "0,23";
Scanner sc = new
Scanner(uSDecimal);
try{
price =
sc.nextFloat();
System.out.println(price);
} catch
(java.util.InputMismatchException e){
e.printStackTrace();
}
try{
sc = new
Scanner(frenchDecimal);
price = sc.nextFloat();
System.out.println(price);
} catch (java.util.InputMismatchException
e){
e.printStackTrace();
}
System.out.println("Switching Scanner to System.in");
try{
sc = new Scanner(System.in);
System.out.println("Enter a float value");
price = sc.nextFloat();
System.out.println(price);
} catch (java.util.InputMismatchException
e){
e.printStackTrace();
}
System.out.print("Enter title:");
String title = sc.nextLine(); //
This line is skipped
System.out.print(title);
}
}
Edit:
This reproduces the issue where the Scanner is waiting for a float value but fails to
trigger when you press
return:
import
java.util.Scanner;
public class IgnoreCRTest
{
public static void main(String[] args) {
Scanner sc =
new Scanner(System.in);
System.out.println("Enter a float
value:");
// On french Locale use , as the decimal separator
float
testFloat = sc.nextFloat();
System.out.println(testFloat);
//sc.skip("\n"); // This doesn't solve the issue
sc.nextLine();
System.out.println("Enter an integer value:");
int
testInt = sc.nextInt();
System.out.println(testInt);
// Will
either block or skip here
System.out.println("Enter a string value
:");
String testString = sc.nextLine();
System.out.println(testString);
}
}
No comments:
Post a Comment