How to cast a value retrieved from text field to int
in Java?
This retrieves a value from JTextField
named BarcodeTxt
:
String barcode = BarcodeTxt.getText();
I want the value in the form of int
rather than String
.
How to cast a value retrieved from text field to int
in Java?
This retrieves a value from JTextField
named BarcodeTxt
:
String barcode = BarcodeTxt.getText();
I want the value in the form of int
rather than String
.
You can use parseInt()
method, from the Integer class, to do that.
// use camelCase to name variables and do not start
// a variable name with uppercased letter - leave that
// for classes
String barCode = BarcodeTxt.getText();
int converted = Integer.parseInt(barCode);
As a reference, check the Javadoc for Integer
.