Results 1 to 9 of 9
  1. #1
    soccermiles is offline Member
    Join Date
    Apr 2010
    Posts
    19
    Rep Power
    0

    Question int[] with multi-digit values: toString()

    How would you put
    int[] intArray={1342,5523,7662,1242,...};

    into a string such that
    String arrayConverted="1342552376621242...";

    --------------------------------------------------------------------------

    Also, if I am using a Swing GUI and I want to make a textArea show a certain String, if I say

    exampleTextArea.setText(String s);

    is there a way I can get it to display the TEXT FROM THE STRING instead of the memory location of the string?:confused:

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    To convert the array to String, you can use String.valueOf(...) to convert each int to a String and either concatenate a single String or use a StringBuilder to append each new String.

    As for displaying a String into a JTextArea, simply calling setText should be all you need to do. If you're getting something else, it may be that you're trying to add a String array or other non-String reference variable to the JTextArea.

  3. #3
    soccermiles is offline Member
    Join Date
    Apr 2010
    Posts
    19
    Rep Power
    0

    Default

    what is String Builder?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    It's actually StringBuilder, and it's a class that allows you to more efficiently concatenate Strings without creating any more objects than are needed. This can get real important if you have to do a lot of String concatenation very fast. Check out the API for more details.

  5. #5
    soccermiles is offline Member
    Join Date
    Apr 2010
    Posts
    19
    Rep Power
    0

    Default

    I tried using the .valueOf thing, and it said something about class "value" being different from class "int".
    Also, it turns out that every time I envoke the .toString() method, thats when it suddenly turns into a memory location instead of the intended string.
    Last edited by soccermiles; 04-24-2010 at 02:48 PM. Reason: more questions

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by soccermiles View Post
    I tried using the .valueOf thing, and it said something about class "value" being different from class "int".
    Also, it turns out that every time I envoke the .toString() method, thats when it suddenly turns into a memory location instead of the intended string.
    Please show us your code and indicate which lines are causing what errors.

  7. #7
    soccermiles is offline Member
    Join Date
    Apr 2010
    Posts
    19
    Rep Power
    0

    Default

    I solved my problem using the StringBuilder object. Thank you very much for your help.
    STRINGBUILDERS IS AWSOME!! BLAARRRGH!!! xD
    Last edited by soccermiles; 04-25-2010 at 03:36 PM. Reason: bad vocab

  8. #8
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    I'll just point out that I've never used StringBuilder in my life :P

    Java Code:
    String s="";
    //integers is an int[]
    for(int add:integers){
         s+=add;
    }
    return s;
    Works just fine :D
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  9. #9
    soccermiles is offline Member
    Join Date
    Apr 2010
    Posts
    19
    Rep Power
    0

    Default

    I got the string to work, its just the output. This is what I did to solve it.
    String blah=("BLAH BLAH BLAH");
    StringBuilder build=new StringBuilder();
    build.append(blah);
    System.out.println(build.toString());

    this also works...

    System.out.println(new String(blah.copy()));

Similar Threads

  1. Replies: 7
    Last Post: 09-26-2011, 11:29 PM
  2. digit spacing
    By puk284 in forum New To Java
    Replies: 3
    Last Post: 06-09-2009, 02:49 PM
  3. Locating a digit within a string
    By ScentOfAWookie in forum New To Java
    Replies: 8
    Last Post: 03-27-2009, 05:23 PM
  4. Java newb Q: 8-digit id
    By 1111FM in forum New To Java
    Replies: 2
    Last Post: 09-29-2008, 02:37 PM

Tags for this Thread

Posting Permissions

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