Reputation: 83
Stuck on attempting to convert a String to an Integer. I'm using libgdx & I've tried a few mothods of doing it & I keep getting null in return.
Here is my most recent method I've tried also the easiest.
String x = textfield_1.getText();
String y = textfield_2.getText();
Integer integerfield_1 = Integer.getInteger(textfield_1.getText(), null);
if (integerfield_1 == null) {
System.out.println("Incorrect Integer (Integer Only)");
} else {
System.out.println("Please Enter The Position");
//TODO Fill GUI Form.
}
Anyone have any tips?
Upvotes: 0
Views: 1716
Reputation: 347194
Take a look at the JavaDocs for Integer.getInteger(String, Integer)
for a sec
Returns:
the Integer value of the property.
Or in longer terms...
Returns the integer value of the system property with the specified name. The first argument is treated as the name of a system property. System properties are accessible through the System.getProperty(java.lang.String) method. The string value of this property is then interpreted as an integer value, as per the Integer.decode method, and an Integer object representing this value is returned.
- If the property value begins with the two ASCII characters 0x or the ASCII character #, not followed by a minus sign, then the rest of it is parsed as a hexadecimal integer exactly as by the method valueOf(java.lang.String, int) with radix 16.
- If the property value begins with the ASCII character 0 followed by another character, it is parsed as an octal integer exactly as by the method valueOf(java.lang.String, int) with radix 8.
- Otherwise, the property value is parsed as a decimal integer exactly as by the method valueOf(java.lang.String, int) with radix 10.
The second argument is the default value. The default value is returned if there is no property of the specified name, if the property does not have the correct numeric format, or if the specified name is empty or null.
This is not doing what you think it is...
Instead you should be using Integer.parseInt(String)
, this will throw a NumberFormatException
when the value of the String
can't be converted to a valid integer...
For example...
String x = textfield_1.getText();
String y = textfield_2.getText();
try {
Integer integerfield_1 = Integer.parseInt(textfield_1.getText());
System.out.println("Please Enter The Position");
//TODO Fill GUI Form.
} catch (NumberFormatException exp) {
exp.printStackTrace();
System.out.println("Incorrect Integer (Integer Only)");
}
Upvotes: 2
Reputation: 201429
If I understand you, then this
Integer integerfield_1 = Integer.getInteger(textfield_1.getText(), null);
Should be something like Integer.parseInt(String)
this -
int integerfield_1 = 0;
try {
integerfield_1 = (x != null) ? Integer.parseInt(x.trim()) : 0;
} catch (NumberFormatException e) {
e.printStackTrace();
}
If you really want to use Integer
(the wrapper), then you could use Integer.valueOf(String)
.
Integer integerfield_1 = null;
try {
integerfield_1 = (x != null) ? Integer.valueOf(x.trim()) : null;
} catch (NumberFormatException e) {
e.printStackTrace();
}
Upvotes: 1