-7

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.

Tomasz Dzieniak
  • 2,765
  • 3
  • 24
  • 42
Serena Gale
  • 109
  • 1
  • 1
  • 6

1 Answers1

2

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.

Bruno Toffolo
  • 1,504
  • 19
  • 24
  • 2
    Please don't answer obviously duplicate questions. See [this](http://meta.stackexchange.com/questions/10841/how-should-duplicate-questions-be-handled) thread on how to handle these. – TayTay Mar 22 '16 at 18:42
  • Didn't know about that -- sorry. – Bruno Toffolo Mar 22 '16 at 18:44