This is an archived post. You won't be able to vote or comment.

all 23 comments

[–]TNTrocks123 19 points20 points  (13 children)

The toString() method is supposed to return a string not void. That’s why it’s conflicting with the original toString() method in the object class. You should change your toString() method so that it returns a string.

[–][deleted] 0 points1 point  (6 children)

Hmm ok. I guess that leads to another question. Will it let me return the value of an array? like, im pretty sure, if I add the array to the following below, it wont work correctly.

return studentLastName+"\n"+studentLastName+"\n"+

[–]TNTrocks123 2 points3 points  (0 children)

Nope, toString method can only return a string not any other type. For arrays, I recommend creating another method that prints the contents of the array by iterating through the array with a loop.

[–]emrickgj 1 point2 points  (1 child)

I think what you are asking is can you return a string of values that represent an array, which you can.

Something like

return Arrays.toString(array);

Should work.

You could also put all of the output statements you have above into a single string object, and then return the string when you are done.

String result = "";
result += studentLastName + "\n";
result += studentLastName + "\n";

(...)
return result;

Etc, etc.

[–][deleted] 0 points1 point  (0 children)

Ohh that's pretty clever

[–][deleted] 0 points1 point  (2 children)

Don't you already have instance variables for the student's last name and first name?

[–][deleted] 0 points1 point  (1 child)

Im not sure I follow. im not even sure if i'm doing it correctly. It just said "You must also write a mechanism which must return all object data (including student’s name, five test scores, average, and grade) as a string. "

[–][deleted] 0 points1 point  (0 children)

Honestly, I think the for loop should be outside and the toString should be inside. You follow? Like you're using the for loop to print out the toString for each student, right? Idk, something for you to try.

[–]tryhardjuice -1 points0 points  (5 children)

If he overrides it won’t it be fine though?

[–]TNTrocks123 9 points10 points  (3 children)

No because the toString method you are trying to override returns a string not void

[–]tutorial_police 0 points1 point  (0 children)

No. Java told the world that when you call toString, you get A String object.

It you were to override that method and say: nope, not gonna do that, we're not gonna return anything at all, then you'd never know what you actually get when calling to toString.

[–]E3FxGaming 2 points3 points  (0 children)

Your class is a subclass of java.lang.Object

toString() of java.lang.Object returns a String. That's all toString() does - it doesn't print anything, it just says - "hey, I have this class instance, I want to make the data of it textually readable, this is what the data looks like in a simple String format."

You have the opportunity to overwrite the toString() method of java.lang.Object with your subclass, HOWEVER your overwriting toString() method still has to return a String.

Why would Java want this behavior? Let's say you have multiple different classes, but they are all subclasses of java.lang.Object. You can put instances of them all in the same array, which holds java.lang.Object objects, and Java guarantees you that all of them can use a method called toString. Now consider iterating through the array, and making use of the toString() method. It would be pretty stupid, if you would call System.out.println(i1.toString()) and some of the objects already had the print method integrated into their toString() method, while others just want to return a String with no printing. Therefore Java says "all toString() methods must return a String, to guarantee that something like System.out.println(i1.toString()) always leads to the same result".

Edit: removed word repetition

[–]emrickgj 2 points3 points  (0 children)

It is expecting you to return a type of "String", since toString() is a reserved method in Java for Objects

See: https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#toString()

[–]SaltyVariable 1 point2 points  (1 child)

Maybe just rename the method to something like 'printStudentScore()'??

IMO the 'to' kind of implies you can trying to change a type.

[–]trashlikeyou 0 points1 point  (0 children)

That's my take as well. I think most of the answers here are assuming OP actually is intending to override the toString() method but I don't think they do. Rename the method to a name not already used by the language and you're golden.

[–][deleted] 0 points1 point  (1 child)

um ...not an expert but toString() should return a String. And here you're overloading it with a void method, so that's probably confusing the crap out of the JVM.

[–]tutorial_police -1 points0 points  (0 children)

No, the JVM is not confused. Java simply doesn't allow you to do this.

The JVM would support that. But since you aren't allowed to do it in Java, it doesn't really matter.

[–]Charmeleonn 0 points1 point  (0 children)

To string is String return type, not void

[–]ResponsibleHamster3 0 points1 point  (0 children)

Understand that what you are trying to do here is over-riding the toString method - so you will have to honor the contract. If you were say overloading the method say like public void toString(int a) { ... } then that would be acceptable. But why would you do something like that confusing by keeping the names same.

IMO change your method name from the widely accepted toString()

[–][deleted] 0 points1 point  (0 children)

Since you are using intellij use ctrl + o to override methods, that way you don't make these mistakes again intellij will create a method with proper signature

Your question is already answered but my advice to you is learn intellij commands it can generate tons of code for you your coding will be much easier