Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

My program displays no output  RSS feed

 
Martin Ndhlovu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I run this program I get this output:

Lists.FirstList@1db9742

Here is the code:



I have added a toString method and I got this output:

[0] = null
[1] = null
[2] = null

Here is my toString method:

public String toString() {
StringBuffer s = new StringBuffer();
int i = 0;
for(Node node = head; node != null; node = node.getNext(), i++) {
s.append("[" + i + "] = " + node.element + "\n");
}
return s.toString();
}
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is not true that you got no output. You got Lists.FirstList@1db9742.

You forgot to override toString() method.
 
Tim Moores
Saloon Keeper
Posts: 3889
91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The class needs a "toString" method if you want to have meaningful output when passing it to System.out.println.
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

When I run this program I get this output:

Lists.FirstList@1db9742

That is the String returned by the FirstList class's default toString() method. It has the classname, an @ and a hex number. Override the toString() method and have the method return the String you want to see.
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have added a toString method and I got this output:

[0] = null
[1] = null
[2] = null

Here is my toString method:

public String toString() {
StringBuffer s = new StringBuffer();
int i = 0;
for(Node node = head; node != null; node = node.getNext(), i++) {
s.append("[" + i + "] = " + node.element + "\n");
}
return s.toString();
}

About your post: please add a reply when you change you code; don't edit your first post.
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think we need to see the code for Node.java. Could you post it in a reply?
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got this output:

[0] = null
[1] = null
[2] = null

Is there a problem with that output? What did you expect it to be?
 
Martin Ndhlovu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As asked by Norm Radder:
I only added two Integers in the list (56) and (25) I expected them to be displayed.

Here is the code for the Node as requested by Knute Snortum:
 
Piet Souris
Rancher
Posts: 1983
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Martin,

well, have a look at this Node constructor:



What happens when you issue: l.add(56)? What node is created?

Another issue is that your head node contains always nulls as its two members. That's how you initiate a new FirstList and that is why you see three nodes in your output, in stead of two.
Can you think of a way to remedy this?

And lastly (but that is for later): your Node class is generic, but FirstList uses the raw version.
 
Martin Ndhlovu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I created anew private class node within the FirstList class itsself and called it MNode. When I complied I got the expected
output. (56) and (25) were displayed.
Here is the private class node:

Now my first list class works fine I can then go on and add more functions like remove() sort() and so on.
Thank you all how assisted.
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is not important syntactically, but by convention this code...

...is written like this:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!