6 Replies - 249 Views - Last Post: 07 February 2013 - 07:44 PM Rate Topic: -----

#1 Heiland  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 11-January 13

Trying to print an array, getting ascii code instead.

Posted 07 February 2013 - 01:26 PM

import java.util.Arrays;

public class TestSort {
    public static void main(String[] args) {
            
        
        Comparable[]  compList;
        compList = new Comparable[4];
        compList[0] = "Bob";
        compList[1] = "Dan";
        compList[2] = "Alice";
        compList[3] = "Chuck";
        System.out.println(compList);
   }
}




This prints out the ascii codes and I cant figure out why. I'm just trying to print out the the names in order. If anyone could help me out I would be very appriciative.

Is This A Good Question/Topic? 0
  • +

Replies To: Trying to print an array, getting ascii code instead.

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,686
  • Joined: 19-March 11

Re: Trying to print an array, getting ascii code instead.

Posted 07 February 2013 - 01:32 PM

Iterate over the array and print the items:

for (String s: compList)  
{
   System.out.println(s);
}


Or assemble a string - create a StringBuilder and iterate over this array, appending each String to the StringBuilder, possibly with some "glue string" in between (but remember not to put the glue string at the end)


EDIT: The "ascii code" you're seeing is the default representation of an object which doesn't override the toString method - it's just a unique String. (well, it's actually unique because it's the address of the object, but that doesn't matter to you)

This post has been edited by jon.kiparsky: 07 February 2013 - 01:34 PM

Was This Post Helpful? 0
  • +
  • -

#3 RozenKristal  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 314
  • Joined: 29-September 12

Re: Trying to print an array, getting ascii code instead.

Posted 07 February 2013 - 01:51 PM

View PostHeiland, on 07 February 2013 - 01:26 PM, said:

import java.util.Arrays;

public class TestSort {
    public static void main(String[] args) {
            
        
        Comparable[]  compList;
        compList = new Comparable[4];
        compList[0] = "Bob";
        compList[1] = "Dan";
        compList[2] = "Alice";
        compList[3] = "Chuck";
        System.out.println(compList);
   }
}




This prints out the ascii codes and I cant figure out why. I'm just trying to print out the the names in order. If anyone could help me out I would be very appriciative.



Isn't because that is not a String array.
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,686
  • Joined: 19-March 11

Re: Trying to print an array, getting ascii code instead.

Posted 07 February 2013 - 01:59 PM

No, you're right. It's an array of Comparables - good eye. So you'll change that a little:


for (Comparable s: compList)

Was This Post Helpful? 0
  • +
  • -

#5 Heiland  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 11-January 13

Re: Trying to print an array, getting ascii code instead.

Posted 07 February 2013 - 02:02 PM

It needs to be in an array when it prints out. So something like..... [Bob, Dan, Alice, Chuck], the ultimate goal of the program is to put the names in alphabetical order but I havent made it that far yet. Also switching it to a String array does nothing.
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,686
  • Joined: 19-March 11

Re: Trying to print an array, getting ascii code instead.

Posted 07 February 2013 - 02:14 PM

If you just want the formatting, that's easy. As I say, you can assemble a String pretty easily using a StringBuilder. Just append things to it:

StringBuilder sb = new StringBuilder();
sb.append("[");
sb.append(compList[0]);
for (int i = 1; i < compList.length; i ++)
{
  sb.append(",");
  sb.append(compList[i]); 

}
sb.append("]");

Was This Post Helpful? 1
  • +
  • -

#7 RozenKristal  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 314
  • Joined: 29-September 12

Re: Trying to print an array, getting ascii code instead.

Posted 07 February 2013 - 07:44 PM

View Postjon.kiparsky, on 07 February 2013 - 01:59 PM, said:

No, you're right. It's an array of Comparables - good eye. So you'll change that a little:


for (Comparable s: compList)


Lol Jon, when you say no, you're right, I am not sure which part I got it correct, I will assume what I said == check!

Edit: nvm, I got what you are saying :)

This post has been edited by RozenKristal: 07 February 2013 - 07:46 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1