-1

am writing a program on linked list in Java but am confused on why the right values are not been added. I want to add the student name and StudentNo together as they for one student. Below is the code.

package linked;
public class PlsWork {
    private String name;
    private int studentNo;

    public PlsWork(String name, int studentNo){
        this.name=name;
        this.studentNo=studentNo;
    } 
public String getname(){
    return name;
}
public int getstudentNo(){
    return studentNo;
}
}

package linked;
import java.util.LinkedList;
public class Linked {

    public static void main(String[] args) {

        LinkedList myLinkedList = new LinkedList();
    myLinkedList.addFirst("A");

    System.out.println(myLinkedList);
        PlsWork  ok = new PlsWork("obinna",3);

        myLinkedList.add(ok);
        System.out.println(myLinkedList);
    }

} 

when i run the code i get the answer below [A, linked.PlsWork@6d06d69c]

Instead of [A,obinna 3]

obinna
  • 27
  • 6

1 Answers1

0

linked.PlsWork@6d06d69c is the reference to your object. That is what toString shows by default. You may want to override toString on your PlsWork-class for example as follows:

class PlsWork {
    ...
    @Override
    public String toString() {
      return name + ' ' + studentNo;
    }
 }
Roland
  • 22,259
  • 4
  • 57
  • 84