I'm new to Java. I want to make student information system, but whenever I used the addStudent
method, I get a list with null values.
This is the student class for getting names telephone numbers and student ID.
import java.util.ArrayList;
public class Student {
private String adSoyad;
private int ogrenciNo;
private String telefonNo;
public Student() {
adSoyad = null;
ogrenciNo = 0;
telefonNo = null;
}
public Student(int ogrenciNo,String adSoyad,String telefonNo){
this.adSoyad = adSoyad;
this.ogrenciNo = ogrenciNo;
this.telefonNo = telefonNo;
}
public Student(Student student){
this.adSoyad = student.adSoyad;
this.ogrenciNo = student.ogrenciNo;
this.telefonNo = student.telefonNo;
}
public void setAdSoyad(String adSoyad) {
this.adSoyad = adSoyad;
}
public void setOgrenciNo(int ogrenciNo) {
this.ogrenciNo = ogrenciNo;
}
public void setTelefonNo(String telefonNo) {
this.telefonNo = telefonNo;
}
public String getAdSoyad() {
return adSoyad;
}
public int getOgrenciNo() {
return ogrenciNo;
}
public String getTelefonNo() {
return telefonNo;
}
@Override
public String toString() {
return (ogrenciNo "," adSoyad "," telefonNo);
}
}
This is the Node class. Methods can be wrong maybe they are too complicated sorry for that. I'm trying to learn Java.
public class Node {
String line;
Node next;
Node prev;
private Student student;
public Node(Student Student)
{
this.student = Student;
}
public Node(String line) {
this.line = line;
}
public void setLine(String line) {
this.line = line;
}
public void setBilgiler(Student bilgiler) {
this.student = bilgiler;
}
public void setNext(Node node) {
next = node;
}
public void setPrev(Node node) {
prev = node;
}
public Student getBilgiler() {
return student;
}
public Node getNext() {
return next;
}
public Node getPrev() {
return prev;
}
public String getLine() {
return line;
}
}
And this is the DoublyLinkedList. I think my main problem is here, but I didn't figure out how I can solve this, so any help is appreciated.
public class DoublyLinkedList {
private Node head;
private Node tail;
public DoublyLinkedList() {
this.head = null;
this.tail = null;
}
public void addLine(String line) {
Node newNode = new Node(line);
if (head == null) {
head = tail = newNode;
head.prev = null;
} else {
head.prev = newNode;
newNode.next = head;
newNode.prev = null;
head = newNode;
}
}
public void addStudent(Student student) {
Node newNode = new Node(student);
if (head == null) {
head = tail = newNode;
head.prev = null;
} else {
head.prev = newNode;
newNode.next = head;
newNode.prev = null;
head = newNode;
}
}
public void display() {
Node current = head;
if (head == null) {
System.out.println("CHECK AGAIN PLEASE!!");
}
while (current != null) {
System.out.print(current.line "\n");
current = current.next;
}
}
}
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
DoublyLinkedList liste = new DoublyLinkedList();
DoublyLinkedList liste2 = new DoublyLinkedList();
File file = new File("C:\\Users\\user1\\OneDrive\\ogrenciler.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String st;
while ((st = br.readLine()) != null) {
liste.addLine(st);
}
Student student1 = new Student(1234, "John Rick", " 1 111-111-1111");
Student obj = new Student();
obj.telefonNolar.add(student1.getOgrenciNo() student1.getTelefonNo());
liste2.addStudent(student1);
liste.display();
liste2.display();
}
}
CodePudding user response:
Your "display" function only prints the "line" member of the nodes irrespective of whether you set the "line" member or not. And your "liste2" variable is populated with "Student" instances and not "String"s. Which means that only the "student" member of the node will be set and the "line" member will be left "null". Hence printing the second list, you will only see nulls, since the "line" members are never set for the nodes.