I have a TextField where I can type values; And I need to convert the value typed to Int and display it with S.O.Println.
EDIT:
I’ve tried now :
try {
int i = Integer.parseInt(myText.getText());
System.out.println("This is an integer " + i);
}
catch (NumberFormatException x) {
System.out.println("Error : "+ x.getMessage());
}
But it’s giving me :
This is an integer 122 // When I type 122
Error : For input string: "122,5" // When I type 122,5
Error : For input string: "122.5" // When I type 122.5
Any ideas.
EDIT:
This is it :
double d = Double.parseDouble(myText.getText());
int i = (int)d;
It appears you are using a comma as a separator, rather than a point. And anyway, you wouldn’t be able to parse it as
int. You’d needDouble.parseDouble(..)Use can use
NumberFormatcorresponding to the current locale in order to parse the input.If you are using SWT (tagged eclipse) – I think you can limit the possible characters by adding a
VerifyListenerto the text field. In your listener, calldoit()on the passed event if the input is allowed.If using AWT (
TextFieldbeing awt’s text component) – then see something like this