0

I am a newbie to java and I am trying to begin with a simple task- list a group of names into alphabetical order. Currently, my code is fine:

import java.util.Arrays;
public class CLASSROOM_SSONG {
    String[] names = {"Joe", "Bob", "Andy"};
    public static void main(String args[]) {
        CLASSROOM_SSONG x =new CLASSROOM_SSONG();
        x.roster();
    }

    public String [] roster() {
        Arrays.sort(names);
        System.out.println(Arrays.toString(names));
        return names;

    }
}

However, this code returns an ARRAY with the brackets, and I prefer there to be names on separate lines without the brackets. This is what I am looking for it to return:

Andy
Bob
Joe

How would I do that? I thought 'println' would give each a separate line, but now I am stuck. I can't think of a way without having to print each name separately, which defeats the purpose completely.

All help would be appreciated!

Oh, by the way, when I search for answers, sometimes I get crazy things with a ton of helper methods. I Prefer simple ones that I can read :)

Darkrai33
  • 51
  • 7
  • This is how Arrays.toString implemented. Please try to iterate on the ArrayList and use System.out.println for each element. You should use println not print so that the output gets printed in new line. – Rupesh Mar 25 '20 at 23:24
  • `System.out.println(String.join("\n", names));` – Johannes Kuhn Mar 25 '20 at 23:25
  • Also, you might want to follow standard naming convention while defining a class name. Something like ShapeClass, in your case ClassRoomSong etc. – Rupesh Mar 25 '20 at 23:27
  • @Darkrai33 you'll find a number of methods [here](https://www.javatpoint.com/how-to-sort-a-list-in-java). The first code sample outputs the list format you want (there's a screenshot) – Rachel Gallen Mar 25 '20 at 23:42

5 Answers5

3

The easiest way is to output the names one after the other. This is possible with a simple for-loop or any iterators.

Simple For-loop:

String[] names = {"Joe", "Bob", "Andy"};

for (String name : names) {
  System.out.println(name);
}
Jerome Wolff
  • 356
  • 3
  • 19
1

You need to go through the array and print out each name with:

for(String name: names) {
  System.out.println(name);
}

or

for (int i=0; i < names.length; i++){
   System.out.println(names[i]);
}
Juan
  • 5,525
  • 2
  • 15
  • 26
1
    String array;

    for(int i=0;i<names.length;++i){
       array+=names[i]+"\n";
    } 
    System.out.println(array);

Maybe this is not the best approach!

Singh
  • 783
  • 1
  • 6
  • 24
0

Try this.

    Arrays.sort(names);  // sort first

    System.out.println(String.join("\n", Arrays.asList(names)));
    // Arrays.asList(names) converts the string into an ArrayList   
Ardit Zotaj
  • 1
  • 1
  • 1
0

You can use JAVA 8 to sort and print each element on a new line.

import java.util.Arrays;
public class CLASSROOM_SSONG {
    String[] names = {"Joe", "Bob", "Andy"};
    public static void main(String args[]) {
        CLASSROOM_SSONG x =new CLASSROOM_SSONG();
        x.roster();
    }

    public void roster() {
        // sort the array and print it to new line
        Arrays.stream(names).sorted().forEach(System.out::println);
    }
}
SSK
  • 3,444
  • 6
  • 32
  • 59