how to implement method to add new Student and display it
hi,
I am at the beginning of my assignment and I have some problems, here is my code:
Code :
import java.io.*;
import java.util.*;
public class StudentDatabase
{
static int STUDENT_SIZE = 20;
Student[] students = new Student[STUDENT_SIZE];
int length = 0; //the current size of the array
Scanner scan = new Scanner(System.in);
public static void main(String[] args)
{
Menu menu = new Menu();
StudentDatabase aStudentDatabase=null;
do{
menu.showMenu();
int aChoice = 0;
aChoice=menu.choice();
while(aChoice==0)
{
System.err.println ( "Unrecognized option, please select again" );
menu.showMenu();
aChoice=menu.choice();
}
if(aStudentDatabase==null)
{
aStudentDatabase = new StudentDatabase();
}
switch(aChoice)
{
case 1:
aStudentDatabase.addStudent();
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
aStudentDatabase.displayStudentDetails();
break;
case 7:
break;
case 8:
System.exit(0);
break;
}
} while(true);
}
public boolean Start()
{
return true;
}
public void addStudent()
{
students[length++] = new Student();
}
public boolean deleteStudent()
{
return true;
}
public boolean addModule()
{
return true;
}
public boolean deleteModule()
{
return true;
}
public boolean assignModule()
{
return true;
}
public void displayStudentDetails()
{
System.out.println("Student Name:" + student.getFirstName() +" "+ student.getLastName() + " Enrollment on:" + student.getEnrollmentDate());
}
public boolean displayModuleDetails()
{
return true;
}
}
Code :
import java.io.*;
import java.util.*;
public class Menu
{
// Scanner for user input
public Scanner scan;
public int choice;
public void showMenu()
{
System.out.println("*****************************");
System.out.println("Welcome to the Student Database!");
System.out.println ( "***********************************" );
System.out.println ( "*************MAIN MENU*************" );
System.out.println ( "***********************************" );
System.out.println ( "Select (1) to Add Student\nSelect (2) to Delete Student\nSelect (3) to Add Module\nSelect (4) to Delete Module\nSelect (5) to Assign Student to Module\nSelect (6) to Display Student Details\nSelect (7) to Display Module Details\nSelect (8) to Exit Program\n");
}
public int choice()
{
scan = new Scanner(System.in);
System.out.print("Please select an option: ");
//Read the input from the user
choice = scan.nextInt();
//Return the user's choice
return choice;
}
}
Code :
import java.io.*;
import java.util.*;
public class Student
{
private String firstName;
private String lastName;
private String enrollmentDate; // better would be to use util.Date not String ??
private Scanner scan;
// default constructor - initializes default attr values
public Student()
{
scan = new Scanner(System.in);
System.out.print("Student Name:");
firstName = scan.next();
System.out.print("Student Surname:");
lastName = scan.next();
System.out.print("Enrollment Date:");
enrollmentDate = scan.next();
}
public Student(String newFirstName, String newLastName, String newEnrollmentDate)
{
this.firstName = newFirstName;
this.lastName = newLastName;
this.enrollmentDate = newEnrollmentDate;
}
//setters
public void setFirstName (String newFirstName)
{
firstName = newFirstName;
}
public void setLastName (String newLastName)
{
lastName = newLastName;
}
public void setEnrollmentDate (String newEnrollmentDate)
{
enrollmentDate = newEnrollmentDate;
}
//getters
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public String getEnrollmentDate()
{
return enrollmentDate;
}
}
1. How I can implement addStudent method in the StudentDatabase class?
2. How to implement the constructor in the Studnet class?
3. How I can implement displayStudentDetails method?
Can you drop me few tips here please?
thanks
E
Re: how to implement method to add new Student and display it
What is the problem you're having with the code, the methods and constructor you mention are in there. Are they not working as expected?
Re: how to implement method to add new Student and display it
well... I am trying to figure out how to increase number of students and add another student to the array, for example:
When I will choose "Add Student" from Menu, I should be able to add a student to Array.
Then I should be able to display it somehow, program is compiling but when I add a student and then I want to display it, it shows:
Please select an option: 6
Exception in thread "main" java.lang.NullPointerException
at StudentDatabase.displayStudentDetails(StudentDatab ase.java:100)
at StudentDatabase.main(StudentDatabase.java:54)
Process completed.
Re: how to implement method to add new Student and display it
With this line:
Code Java:
System.out.println("Student Name:" + student.getFirstName() +" "+ student.getLastName() + " Enrollment on:" + student.getEnrollmentDate());
Your calling the Student methods on variable student but you haven't initialised it anywhere so you're calling methods on an object that doesn't exist which is why you're getting NullPointerException
You're adding the students to the array so use the Student objects in the array to call methods on
Code Java:
students[length].getFirstName()
and so on
Re: how to implement method to add new Student and display it
ok thanks,
I have changed the addStudent method to:
Code :
public void addStudent()
{
students[length].getFirstName();
students[length].getLastName();
students[length].getEnrollmentDate();
}
and also I have added:
then
Code :
public void displayStudentDetails()
{
System.out.println("Student Name:" + aStudent.getFirstName() +" "+ aStudent.getLastName() + " Enrollment on:" + aStudent.getEnrollmentDate());
}
now I am getting an error when:
1. Select Add student
Please select an option: 1
Exception in thread "main" java.lang.NullPointerException
at StudentDatabase.addStudent(StudentDatabase.java:75 )
at StudentDatabase.main(StudentDatabase.java:38)
2. display Student details:
Please select an option: 6
Exception in thread "main" java.lang.NullPointerException
at StudentDatabase.displayStudentDetails(StudentDatab ase.java:102)
at StudentDatabase.main(StudentDatabase.java:54)
Those errors are thrown cos object does not exist again...?
Re: how to implement method to add new Student and display it
The add method was ok as it was except maybe you'd want to put it like this
Code Java:
public void addStudent()
{
students[length] = new Student();
length++;
}
It was your display student method originally that was causing the problem. You don't really need the aStudent variable, you can call the methods from the objects in the array
Code Java:
public void displayStudentDetails()
{
System.out.println("Student Name:" + students[length-1].getFirstName() +" "+ students[length-1].getLastName() + " Enrollment on:" + students[length-1].getEnrollmentDate());
}
It needs to be length-1 because when you first create a Student and add it to the array, it adds it at position length which is 0 but you then add 1 to length. So students[length] would be students[1] and there's nothing in that part of the array so you get your null pointer exception.
Re: how to implement method to add new Student and display it
:)
It is working! :) thanks so much! I have even changed some stuff for example:
Code :
public void displayStudentDetails()
{
for (int i=0; i<length; i++)
{
System.out.println("Student Name:" + students[i].getFirstName() +" "+ students[i].getLastName() + " Enrollment on:" + students[i].getEnrollmentDate());
}
}
so if I add more students all students from database are getting displayed.
I have now created class called module, and I did the same with it, so I am able to add new module and display it.
The main question now is how I can assign a student to the module, where every student can be only max assigned to 3 modules (assuming I can have only 6 max modules) :O
I was thinking about making two dimension array [20] (this is a max Students) [6] (max Modules) and just say true/false if student is assigned to it?
or maybe I should think about something different? :)