Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: JTextField returns a string, but I want an int

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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).


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default 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().
    Last edited by pbrockway2; February 7th, 2012 at 11:38 PM.

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    10
    My Mood
    Lurking
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default 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...

  4. #4
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: JTextField returns a string, but I want an int

    Thanks a lot that helps a ton

Similar Threads

  1. PrintJobListener Always Returns printJobNoMoreEvents
    By Basak in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 28th, 2011, 06:50 AM
  2. Javascript function returns NaN
    By jai in forum Other Programming Languages
    Replies: 2
    Last Post: April 19th, 2011, 02:31 AM
  3. returns all suggestions
    By mario_tim in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 26th, 2011, 04:51 AM
  4. How web service returns xml?
    By nikos in forum Web Frameworks
    Replies: 2
    Last Post: October 7th, 2010, 12:50 PM
  5. Passing JTextField as String parameter
    By Christophe in forum Java Theory & Questions
    Replies: 3
    Last Post: April 11th, 2010, 05:32 AM