-2

Why does print toString() doesn't print Array Class member objects? It says "cannot return a void result". What type of result does it want when my ArrayList private objects contain values.

Eg: In Person class-

public class Person { 

    private ArrayList <String> name;

    private ArrayList <Integer> number;

    public Person(ArrayList <String> thatName, ArrayList <Integer> thatNumber) 
    {
        name = new ArrayList <String>(thatName);
        number = new ArrayList <Integer>(thatNumber);
    }

    public ArrayList<String> getName() 
    {
        return name;
    }

    public ArrayList<Integer> getNumber() 
    {
        return number;
    }

    public String toString() 
    {
        return System.out.println(name + "" + number); //Gives error "cannot return a void result"    
    }   

Also, can I make String.format(,) work in this case? I tried, but it doesn't work as the ArrayList object types are not primitive data types.

EDIT:

Here is how I am calling Person class but toString() not printing anything.

import java.util.ArrayList;
public class PersonTest {

public static void main(String[] args) {

    ArrayList <String> nam = new ArrayList <String>();
    ArrayList <Integer> numb = new ArrayList <Integer>();

    nam.add("Zeus");
    numb.add(99);

    Person pr = new Person(nam, numb);

    pr.toString();

}

}
Zeus
  • 59
  • 6
  • 1
    Those who are down voting please leave a reason also, if you are a human being. – Zeus Nov 22 '16 at 06:05
  • Read the documentation. Learn the Java API. Understand the very self-explanatory error message... *Question does not show research effort* is on the hover text of the down arrow. – OneCricketeer Nov 22 '16 at 06:07
  • 3
    Well, first, this won't even compile ... so the title **print toString() doesn't print ArrayList Class member objects** is not correct. – AxelH Nov 22 '16 at 06:26
  • @AxelH, how would it have worked? – Zeus Nov 22 '16 at 06:28
  • You are probably running the last compilable code ... Error are not only there to warn you. – AxelH Nov 22 '16 at 06:31

2 Answers2

2

You want to return a string, println is a void method.

Somewhat along the lines of this Python question -- How is returning the output of a function different than printing it?

Change

return System.out.println(name + "" + number)

to return name + "" + number;

Also, can I make String.format(,) work in this case?

Sure. return String.format("Names: %s\nNumbers: %s", name, number);

Community
  • 1
  • 1
OneCricketeer
  • 151,199
  • 17
  • 111
  • 216
  • Thanks, but it's not working. I posted my calling method and its class. Please help. – Zeus Nov 22 '16 at 06:23
  • 2
    @Zeus, `pr.toString()` won't do anything expect getting a String value. You need to print the value via `System.out.print(String)`. And this can't be done in the `toString()` (not on the return line at least) method because this need to return a String. Read the full answer ! – AxelH Nov 22 '16 at 06:33
2

System.out.println is a void, therefore can't return anything. What you should do is :

public String toString()
{
  return name + " " + number;
}

and in the caller, use person instance to print :

System.out.println(person.toString());

or below, since printing an object will automatically invoke the toString() method.

System.out.println(person);
Rudy
  • 6,720
  • 10
  • 46
  • 81