JTextField returns a string, but I want an int
I feel like I'm overlooking something, but I have been stuck with this problem for a few days. I have a variable that i want to represent the value in a JTextField, the problem is my variable is an int, and JTextField.getText() returns a string. Obviously i can't just cast it( or can I?)... so do I have to go in and change each letter in the JTextField into hexadecimal and then into a number value, or is there a method or class that does this for me?
Thank you for your help(even if this was easy for you).
Re: JTextField returns a string, but I want an int
No, you can't cast int->String. In fact if you think about it there is more than mere "casting" involved as numerals (the string representation of numeric values) involve the notion of "base" which is no part of a numeric value.
Java does provide a method for just this purpose! Check out theInteger.parseInt() API docs. Also there is a page on Converting between Numbers and Strings in Oracle's Tutorial that suggests another set of (equivalent) methods - valueOf().
Re: JTextField returns a string, but I want an int
The java.lang.Integer class has a lot of built in tools you can use... The eclipse IDE will list many by typing Integer. and scroll through the list...
Re: JTextField returns a string, but I want an int
Thanks a lot that helps a ton