0

enter image description hereHi I'm trying to align output to the console window in Java using NetBeans.

ATM I have a collection of MP3Tracks (classes) in an arraylist. The user is able to sort the track by artist, album & track name etc after the sort the reordered list is via the console:

System.out.println(vector);

My MP3Track class overrides toString():

@Override
public String toString(){
    return(String.format("Track No: %-8s Track Title: %-25s Artist Name: %-15s Album Name: %-20s Track Length: %-10s", trackNo , trackTitle, artistName, albumName, trackLength + "\n"));

}

This solution works for all but the first line which is line and the first and last digit output is []. I would like all lines to line up and to remove both [] at the beginning and end. I'm a first year Software Engineering student and this in my first assignment in Java so please excuse my inexperience!

I would very much appreciate some suggestion here.

Many thanks in advance..

1
  • 1
    Consider showing/posting your current output and your desired output. Commented Nov 3, 2013 at 2:05

2 Answers 2

2

You are printing your vector using

System.out.println(vector);

it calls vector's toString() method to get its String representation and Java's String representation for Collections is formatted like:

[element1.toString(), element2.toString, ... ]

You can just iterate over your vector and print your MP3Tracks:

for (MP3Track mp3Track : vector) {
    System.out.println(mp3Track);
}

And remove the \n at the end of your the string representation of MP3Track, it would be nicer that way.

Alternatively, you create a subclass of Vector class and override its toString() method and use it but that would be overkill imo.

Sign up to request clarification or add additional context in comments.

Comments

0

You should not rely on toString() to present data to the end-user. toString is meant for debug purposes. The braces are there because the array's toString prints them.

When you want to present data to the user, you should use an external viewer class, that prints things just like they should be printed, according to you. This, or you can create a List implementation that delegates to a JavaSE collection and overrides toString, but I'd really dislike this way of doing things.

Something like:

public class TrackViewer {
    private static final String FORMAT
            = "Track No: %-8s Track Title: %-25s "
            + "Artist Name: %-15s Album Name: %-20s "
            + "Track Length: %-10s\n";

    private String getTrackLine(Track t) {
        return String.format(FORMAT,
                t.getTrackNo(),
                t.getTrackTitle(),
                t.getArtistName(),
                t.getAlbumName(),
                t.getTrackLength());
    }

    public void listTracks(Iterable<Track> tracks) {
        StringBuilder bdr = new StringBuilder();

        for (Track t : tracks) {
            bdr.append(getTrackLine(t));
        }

        System.out.print(bdr.toString());
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.