Results 1 to 12 of 12
  1. #1
    mDennis10 is offline Member
    Join Date
    Jul 2011
    Posts
    76
    Rep Power
    0

    Default How to display integers in textField

    i want to know how can i convert an integer into a string, so it can be displayed in textField. I tried parsing but it did not work

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    354
    Rep Power
    5

    Default

    If you have an integer you want to convert to a string, use the toString method. For example:

    int x = 10;
    String myString = Integer.toString(x);

    Then, to put that string into a textfield, assuming you mean something like a JTextField, simply do textFieldName.setText(myString);

    There you go.

  3. #3
    mDennis10 is offline Member
    Join Date
    Jul 2011
    Posts
    76
    Rep Power
    0

    Default

    Quote Originally Posted by sehudson View Post
    If you have an integer you want to convert to a string, use the toString method. For example:

    int x = 10;
    String myString = Integer.toString(x);

    Then, to put that string into a textfield, assuming you mean something like a JTextField, simply do textFieldName.setText(myString);

    There you go.
    want import does that method belong to

  4. #4
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    354
    Rep Power
    5

    Default

    you don't need any imports to do the integer to string conversion, its just a part of the Integer class.

    In what context are you using your textField. Is this a web application?
    Last edited by sehudson; 08-11-2011 at 07:47 PM.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    the integer to string conversion, its just a part of the String class.
    That's strange, your code was: Integer.toString(x);
    The String class also has a method to convert an int to a String.

  6. #6
    mDennis10 is offline Member
    Join Date
    Jul 2011
    Posts
    76
    Rep Power
    0

    Default

    Quote Originally Posted by sehudson View Post
    you don't need any imports to do the integer to string conversion, its just a part of the Integer class.

    In what context are you using your textField. Is this a web application?
    Desktop Application

  7. #7
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    354
    Rep Power
    5

    Default

    Ok.

    So yes, once you convert the Integer to a String, just do the textfieldName.setText(myString) and that should work. If you are using Netbeans, right click anywhere inside the class file and select Fix Imports and it will import any necessary packages.

    See if that works for you.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by sehudson View Post
    If you have an integer you want to convert to a string, use the toString method. For example:

    int x = 10;
    String myString = Integer.toString(x);
    That won't work; the toString( ... ) method in the Integer class isn't static. Better use the String.valueOf( ... ) method; that one is a static method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    From the Integer class's API doc:
    public static String toString(int i)

    Java Code:
           String two = Integer.toString(2);
           System.out.println("two=" + two);  // two=2

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    From the Integer class's API doc:
    public static String toString(int i)

    Java Code:
           String two = Integer.toString(2);
           System.out.println("two=" + two);  // two=2

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Norm View Post
    From the Integer class's API doc:
    public static String toString(int i)

    Java Code:
           String two = Integer.toString(2);
           System.out.println("two=" + two);  // two=2
    My bad; I goofed, sorry about that.

    kind regards,

    Jos <-- *grmbl* stupid confusing name, *mumble* *mumble*
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default

    For Integer and String imports are not needed as they are both part of java.lang.
    If you aren't programming in Java, well that's just too bad.
    I'd rather be using Ubuntu.

Similar Threads

  1. Replies: 1
    Last Post: 12-01-2010, 06:35 PM
  2. Replies: 1
    Last Post: 05-31-2010, 08:46 AM
  3. Set of Integers
    By rsjava24 in forum New To Java
    Replies: 7
    Last Post: 01-28-2010, 10:29 AM
  4. Replies: 8
    Last Post: 12-29-2008, 07:12 AM
  5. Replies: 7
    Last Post: 04-28-2008, 04:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •