Results 1 to 7 of 7
  1. #1
    Jerome is offline Member
    Join Date
    Dec 2008
    Posts
    3
    Rep Power
    0

    Default [SOLVED] Ljava.lang.String;@923e30 at runtime

    Hi,

    I'm new here, and to Java, and having quite some trouble with this, hope someone can help.

    I created an object array of points (x,y), called "lijst". Now I want to print that array using a toString() method, it has to be printed like this: <PuntenLijst[ PP , PP , … ]>. "PP" are the point objects, they have the following toString() method in class "Punt", which works just fine:

    Java Code:
    public String toString()
      {
        String punt = ("<Punt(" + xCoord + "," + yCoord + ")>");
        return punt;
      }
    This is the toString() method in class "PuntenLijst" I have so far for the array of points:

    Java Code:
    public String toString()
      {
       String punt = "";
       String[] punten = new String[lijst.length];
       int keer = 0;
         for (int i = 0; i < lijst.length; i++)
         {
           keer++;
           for (int j = 0; j < keer; j++)
           {
           punten[j] = lijst[i].toString();
           }
         }
         String lijst = "<PuntenLijst[ " + punten + " ]>";
         return lijst;
       }
    "lijst" is the array of points.

    It compiles just fine, but when I run it, I get "<PuntenLijst[ Ljava.lang.String;@923e30 ]>"

    I hope anyone can help. If you need more info, just yell, I'm new at this stuff.

    Ciao.
    Attached Files Attached Files
    Last edited by Jerome; 12-31-2008 at 12:39 AM. Reason: Attach code

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    <PuntenLijst[ Ljava.lang.String;@923e30 ]>
    When you try to print an array you get the Object.toString method which returns the hex address of the array in memory.
    Java Code:
      //<PuntenLijst[ PP , PP ,  ]>
      public String toString()
      {
        String punts = "";
        for (int i = 0; i < aantal; i++)
        {
          punts += lijst[i].toString();
        }
        String lijst = "<PuntenLijst[ " + punts + " ]>";
        return lijst;
      }

  3. #3
    Jerome is offline Member
    Join Date
    Dec 2008
    Posts
    3
    Rep Power
    0

    Default

    Wow.

    I had the same code at first, but I had
    Java Code:
    punts = lijst[i].toString();
    instead of
    Java Code:
    punts += lijst[i].toString();
    but then it only prints the last point of the array. What does that += do exactly?

    Thanks a lot.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    What does that += do
    It's shorthand for
    Java Code:
    s = s + lijst[i].toString();

  5. #5
    Join Date
    Dec 2008
    Location
    Italy
    Posts
    79
    Rep Power
    0

    Default

    Not clear. You said:
    Quote Originally Posted by Jerome
    "lijst" is the array of points.
    then you coded
    Java Code:
    ...
    String lijst = "<PuntenLijst[ " + punten + " ]>";
    ...
    before the return statement.

    However you solution could probably be
    Java Code:
    public String toString() {
       StringBuffer output = new StringBuffer("<PuntLijst [ ");
       for(Punt p : lijst) {
          output.append(p.toString() + ", ");
       }
       int x = output.lastIndexOf(",");
       output.delete(x, x+2);
       output.append("]>");
       return output.toString();
    }
    Sorry I hadn't seen the hardwired solution. His one is correct
    Last edited by raffaele181188; 12-30-2008 at 11:45 PM.

  6. #6
    Jerome is offline Member
    Join Date
    Dec 2008
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by hardwired View Post
    What does that += do
    It's shorthand for
    Java Code:
    s = s + lijst[i].toString();
    Thanks a lot, it works like a charm now.


    Quote Originally Posted by raffaele181188 View Post
    Not clear. You said:

    then you coded
    Java Code:
    ...
    String lijst = "<PuntenLijst[ " + punten + " ]>";
    ...
    before the return statement.
    I made a Punt[] object earlier, which I named lijst, and later on I made the String lijst. Indeed not very clear from my explanation.

    Thanks both for your input.

  7. #7
    Join Date
    Dec 2008
    Location
    Italy
    Posts
    79
    Rep Power
    0

    Default

    OK, but you should avoid that, because it makes confused code. Just find another name

    Bye

Similar Threads

  1. Replies: 2
    Last Post: 09-18-2008, 08:30 AM
  2. Replies: 5
    Last Post: 09-03-2008, 06:29 PM
  3. why my url string is changed in runtime ?
    By aneuryzma in forum New To Java
    Replies: 1
    Last Post: 08-13-2008, 10:46 PM
  4. How to cast an Object into a specific type (Integer/String) at runtime
    By mailtogagan@gmail.com in forum Advanced Java
    Replies: 2
    Last Post: 12-03-2007, 02:04 PM
  5. Can't convert java.lang.String to int.
    By Albert in forum AWT / Swing
    Replies: 2
    Last Post: 07-13-2007, 06:05 PM

Posting Permissions

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