Hack Forums
[Java] Java Linked List isn't displaying strings correctly - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Java] Java Linked List isn't displaying strings correctly (/showthread.php?tid=5794392)



Java Linked List isn't displaying strings correctly - gianni - Feb 3, 2018

Main Class:
Spoiler (Click to View)
myHero Class:
Spoiler (Click to View)
When I add a hero to the list it adds correctly but when I display the list it doesn't print as the 2 strings but as some random characters with the class name.

Picture of what is happening:
i.imgur.com/PpFDf73.png


RE: Java Linked List isn't displaying strings correctly - GenericSkid - Feb 3, 2018

You do not override the toString() method. I think you attempted but misunderstood how.

PHP Code

<?php 
// current code
// toString is not supposed to have parameters. You're supposed to just use field references.
 
  public String toString(String name, String date)
 
  {
 
      return (name + "" + date);
 
  } 
PHP Code

<?php 
// correct code 
 
  @Override
   
public String toString()
 
  {
 
      return name + " " + date;
 
  } 



RE: Java Linked List isn't displaying strings correctly - gianni - Feb 3, 2018

(Feb 3, 2018 01:53 PM)GenericSkid Wrote:  You do not override the toString() method. I think you attempted but misunderstood how.

PHP Code

<?php 
// current code
// toString is not supposed to have parameters. You're supposed to just use field references.
 
  public String toString(String name, String date)
 
  {
 
      return (name + "" + date);
 
  } 
PHP Code

<?php 
// correct code 
 
  @Override
   
public String toString()
 
  {
 
      return name + " " + date;
 
  } 

Thank you for fixing my silly mistake. 🤝