I made a list for my teachers and print out the list. The list prints but the problem is that my list prints out null for all values in my list. It gives me null for firstname, last name, id and course. What am I doing wrong??? How can I fix this? I want to be able to print out my actual values in my list of teachers. I don't see what could be the issue if I'm being totally honest. I add the first name, last name, id and course to the teacher list correctly. So what am I missing????
Main.java code:
package SchoolSystem;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
public class main{
public static void main(String[] args) throws InterruptedException {
int ch; //user choice
teacher teachers = new teacher();
student students = new student();
//add teachers
List<teacher> teach = new ArrayList<>();
teach.add(new teacher(teachers.first_name, teachers.last_name, teachers.teacher_id, teachers.course));
Scanner sc = new Scanner(System.in);
loop : while (true) {
//menu
System.out.println("");
System.out.println("1: Add Teacher"); //user can add a teachers name, id, and course
System.out.println("2: Add Student"); //user can add a students name, id, courses, and GPA
System.out.println("3: All Teachers"); //user can access teacher list and change items
System.out.println("4: All students"); //user can access student list and change items
System.out.println("5: Exit Program");
System.out.print("Enter your choice: ");
ch = sc.nextInt();
System.out.println("");
switch (ch) {
case 1:
System.out.println("Enter teacher's first name: ");
teachers.first_name = sc.next();
System.out.println("Enter teacher's last name: ");
teachers.last_name = sc.next();
System.out.println("Enter teacher's id: ");
teachers.teacher_id = sc.next();
System.out.println("Enter teacher's course: ");
teachers.course = sc.next();
break;
case 2:
System.out.println("Enter student's first name: ");
students.first_name = sc.next();
System.out.println("Enter student's last name: ");
students.last_name = sc.next();
System.out.println("Enter student's id: ");
students.student_id = sc.next();
System.out.println("Enter student's course: ");
students.course = sc.next();
break;
case 3:
System.out.println("-----------------------------------------------------------------------------");
System.out.printf("%1s s %5s %5s", "FIRSTNAME", "LASTNAME", "ID", "COURSE");
System.out.println();
System.out.println("-----------------------------------------------------------------------------");
for(teacher teacher: teach){
System.out.format("%1s s %5s %5s",
teacher.getFirstName(), teacher.getLastName(), teacher.getId(), teacher.getCourse());
System.out.println();
}
System.out.println("-----------------------------------------------------------------------------");
break;
case 4:
//null
break;
case 5:
/*
System.out.println("Exiting Program....");
TimeUnit.SECONDS.sleep(3);
System.out.println("Goodbye!");
break loop;
*/
break;
default:
//System.out.println("Invalid choice! Please enter an option (1 - 5)");
}
}
}
}
Teacher.java code:
package SchoolSystem;
public class teacher {
public teacher() {
//null
}
public String first_name;
public String last_name;
public String teacher_id;
public String course;
public teacher(String first_name, String last_name, String teacher_id, String course) {
this.first_name = first_name;
this.last_name = last_name;
this.teacher_id = teacher_id;
this.course = course;
}
//return firstname
public String getFirstName() {
return first_name;
}
//return lastname
public String getLastName() {
return last_name;
}
//return teacherId
public String getId() {
return teacher_id;
}
//return course
public String getCourse() {
return course;
}
}
CodePudding user response:
in Teacher Class you must add setters
public class Teacher {
public Teacher() {
//null
}
public String first_name;
public String last_name;
public String teacher_id;
public String course;
public Teacher(String first_name, String last_name, String teacher_id, String course) {
this.first_name = first_name;
this.last_name = last_name;
this.teacher_id = teacher_id;
this.course = course;
}
//return firstname
public String getFirstName() {
return first_name;
}
public void setFirstName(String firstName) {
this.first_name = firstName;
}
//return lastname
public String getLastName() {
return last_name;
}
public void setLastName(String lastName) {
this.last_name = lastName;
}
//return teacherId
public String getId() {
return teacher_id;
}
public void setId(String id) {
this.teacher_id = id;
}
//return course
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
}
in Main delete this line
teach.add(new Teacher(teachers.getFirstName(), teacher.getLastName(), teacher.getId(), teacher.getCourse()));
and write:
int ch; //user choice
//add teachers
List<Teacher> teach = new ArrayList<>();
Scanner sc = new Scanner(System.in);
// teach.add(new Teacher(teachers.getFirstName(), teacher.getLastName(), teacher.getId(), teacher.getCourse()));
loop : while (true) {
//menu
System.out.println("");
System.out.println("1: Add Teacher"); //user can add a teachers name, id, and course
System.out.println("2: Add Student"); //user can add a students name, id, courses, and GPA
System.out.println("3: All Teachers"); //user can access teacher list and change items
System.out.println("4: All students"); //user can access student list and change items
System.out.println("5: Exit Program");
System.out.print("Enter your choice: ");
ch = sc.nextInt();
System.out.println("");
switch (ch) {
case 1:
Teacher teachers = new Teacher();
System.out.println("Enter teacher's first name: ");
teachers.setFirstName(sc.next());
System.out.println("Enter teacher's last name: ");
teachers.setLastName(sc.next());
System.out.println("Enter teacher's id: ");
teachers.setId(sc.next() );
System.out.println("Enter teacher's course: ");
teachers.setCourse (sc.next());
teach.add(teachers);
break;
case 2:
// .....
use setters to add values and add "teachers" oject to List
teach.add(teachers);
CodePudding user response:
You are having OOP issues with your code.
teacher teachers = new teacher();
...
List<teacher> teach = new ArrayList<>();
teach.add(new teacher(teachers.first_name, teachers.last_name, teachers.teacher_id, teachers.course));
This snippet takes a new instance of a teacher
and then creates a brand new instance of teacher
to add to the list. These are 2 distinct objects. I'm assuming you are coming from C or C and you are thinking of them as pointers? Fortunately, that's not how Java works.
You can simply add the "teachers" variable to the "teach" list. Then when you later update the "teachers" object, the data will be available in the "teach" list.
The caveat to this is you need to instantiate a new object for "teachers" inside the loop, as well as moving the modified teach.add(teachers);
into the loop.
List<teacher> teach = new ArrayList<>();
List<student> stud = new ArrayList<>();
Scanner sc = new Scanner(System.in);
loop : while (true) {
...
switch (ch) {
case 1:
teacher teachers = new teacher();
...
teach.add(teachers);
break;
case 2:
student students = new student();
...
stud.Add(students);
break;
...
Now you have a new teacher
object for every teacher as well as a list containing only valid data, rather than empty objects.
This works because "teach" now contains a reference to the instance of "teachers". This is similar to the pointers of C and C I mentioned earlier. In this case, you have a reference to the whole object, not to the individual properties of the object you were trying to have earlier.
Think of an object as a piece of paper and a list as a box. You can put all kinds of data on the paper, then put the piece of paper in the box. When you need a new object, you get a new piece of paper, write the data on it, and add it to the box. Then when you want to update an object, you find it in the box and update the data. It's that simple. You can have a complex object where it has instances of other objects in it, but that's beyond the scope of your question.
On a side note
Classes teacher
and student
should be capitalized as Teacher
and Student
. The list of Teachers
should be named "teachers" and a single Teacher
object should be named "teacher", or something that won't be confused with the class name. In general, lists should be plural and a singular instance should be singular.