user180708
user180708

Reputation: 427

Returning Objects with toString() method

I have a main method that is calling a class method;

CourseListQ2 list = new CourseListQ2();

list.addCourse(new Course("Structure and Modelling", "Chemistry", 1));
list.addCourse(new Course("Design in Engineering", "Engineering", 1));
......

In this class that I wrote, I created an array of objects from the main method, and subsequently expanded it and deleted values I didn't need. Then, I wrote a toString method that was suppose to return each object on a new line:

public String toString(){
      return  Arrays.toString(arrayCourses) ;}

However, it's returning the entire array on a single line. So obviously, I'm misinterpreting what this method does exactly.

I have two ideas, either I'm suppose to try to return each as an object, like getName().toString(arrayCourses) + getYear().toString(arrayCourses) + "\n",

Or, maybe I'm suppose to create a loop that adds a new line. Yet I think this defeats the purpose of having the toString method, and it still returns a '[' at the beginning of the output.

So, could anyone explain why this is happening and perhaps suggest a better approach?

Upvotes: 3

Views: 3070

Answers (4)

ashwin
ashwin

Reputation: 55

In Java every object has a toString() method (if you didn't write one there is a default one provided)

If we try to run the toString() on a collection object it runs the toString() method on all the objects and prints them between [], separated by a ','.

If you would like to have your own implementation then I suggest to override the toString() of the method to print the values the way you want

Upvotes: 0

Tarik
Tarik

Reputation: 5021

In your example Arrays.toString(arrayCourses) you are calling the toString() method of the Arrays class, below is what Java Docs says about that method:

Returns a string representation of the contents of the specified array. The string representation consists of a list of the array's elements, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (a comma followed by a space). Elements are converted to strings as by String.valueOf(float). Returns "null" if a is null.

For the approach i would suggest what @dasblinkenlight adviced.

Upvotes: 1

sodik
sodik

Reputation: 4683

There is nothing wrong on using loop in toString() method. However if you use Java8 you can use String.join. So if you want to have just each element on one line use

return String.join('\n', arrayCourses);

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726569

I'm misinterpreting what this method does exactly.

Arrays.toString formats an array as a single String, with string representations of individual elements separated by commas.

Either I'm suppose to try to return each as an object, like getName().toString(arrayCourses) + getYear().toString(arrayCourses) + "\n"

That's not going to solve the problem entirely, because the commas inserted by Arrays.toString would remain there. Generally, it's not a good idea to append '\n' in your toString implementation, because it makes an assumption about the way the string is going to be used by the callers.

Or, maybe I'm suppose to create a loop that adds a new line.

That's a better idea. Use StringBuilder, and call its append method twice - first for the object, and second for the \n character.

Note: rather than appending '\n' directly, use System.getProperty("line.separator"); to get the system-specific line separator.

StringBuilder res = new StringBuilder();
String newLine = System.getProperty("line.separator");
for (Course c : arrayCourses) {
    res.append(c);
    res.append(newLine);
}

Upvotes: 0

Related Questions