Join 150,381 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,317 people online right now. Registration is fast and FREE... Join Now!
I am trying to create a student information management system. When I select number 1 I get a return of null. From that point I can only access the records of Tom Tucker. What am i missing here?
public static void main(String[] args) throws IOException {
//create a bufferedReader object BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String choiceString; int choice;
//create an array variables to hold student information int students=5; final String stuFullNameArray[]={"Tom Tucker","Larry Fast","Martin Slow","Peter Fellows","Don Dinkin"}; final String stuDOBArray[]={"March 11","Oct 10","Feb 7","Aug 17","May 19"}; final int stuGradArray[]={2009,2010,2008,2010,2015}; final double stuSSNArray[]={111111111,222222222,333333333,444444444,555555555}; final double stuGPAArray[]={3.5,4.0,3.8,3.1,2.9}; final String classInformationArray[]={"Peter Clay","Java Programming"}; final String stuInformationArray[]= new String [5];
//Ask for user input for (int i=0; i < 4;i ++) { do { //print out a menu System.out.println(); System.out.println("\n------- Student Management System ----------"); System.out.println("\n1. Student Information"); System.out.println("\n2. Class Enrollment Information"); System.out.println("\n3. Graduation Year"); System.out.println("\n4. Current G.P.A"); System.out.println("\n5. Quit"); System.out.println("\nEnter choice [1,2,3,4,5]: ");
System.out.flush(); choiceString = in.readLine();
//convert string to integer choice = Integer.parseInt(choiceString);
switch (choice) { //the choices go here - print the details
case 1: for (int x=0; x<5; x++) { System.out.println("\nStudent Information: ["+ x +"]" + stuInformationArray[x]);break; }break; case 2: for (int x=0; x<4; x++) { System.out.println("\nClass Enrollment Information: " + classInformationArray[x]); break; }break; case 3: for (int x=0; x < 5; x++) { System.out.println("\n" + stuFullNameArray[x] + "'s Graduation Year is: " + stuGradArray[x]); break; }break; case 4: for (int x=0; x <5; x++) { System.out.println("\n" + stuFullNameArray[x] + "'s Current G.P.A. is: " + stuGPAArray[x]); break; }break; case 5: //exit System.exit(0); default: System.out.println("\nPlease choose a number between 1-5 only \n"); } } while(choice!= 5);
}//End of class statement } }
Mod Edit: Please use code tags when posting your code. Code tags are used like so =>
The problem is down in your switch statement. I am not sure why you have a for loop there, but the reason you are getting null there for option 1 is because that string array stuInformationArray is set to null. You didn't load info into that.
Then the reason you can only access Tom Tucker's info because in each case of your switch statement you start a loop which starts at 0 (student 1), reads the appropriate array and then breaks immediately.
So each of your for loops in your switch statement is going to only execute once because your break.
How do we solve this? Well, look at the way the typical system works. Do you just specify "student information" and get the information from the appropriate student? No, you have to enter two pieces of info, the option you want from the menu and then the student you want.
These two pieces of info are going to determine 1) The choice they selected from the menu which accesses the appropriate array and 2) The student which will tell you which element of that array to access.
So lets say we wanted Larry's grad year. We would enter choice 3 which would give us data from the stuGradArray and then we would chose Larry by entering 1, the subscript of Larry in that stuGradArray.
Rework your switch statement and try to stay away from executing a loop in the cases. I think you will find you don't need a loop here unless you wanted to loop through all students. If that is the case, don't put a break in your loop because that is only going to stop your for loop after the first iteration.
port java.io.*; import java.io.IOException; import java.util.*; import java.lang.*; import java.lang.String;
public class ManagementSystem {
public static void main(String[] args) throws IOException {
//create a bufferedReader object BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String choiceString; int choice;
//create an array variables to hold student information int students=5; final String stuFullNameArray[]={"Tom Tucker","Larry Fast","Martin Slow","Peter Fellows","Don Dinkin"}; final String stuDOBArray[]={"March 11","Oct 10","Feb 7","Aug 17","May 19"}; final int stuGradArray[]={2009,2010,2008,2010,2015}; final double stuSSNArray[]={111111111,222222222,333333333,444444444,555555555}; final double stuGPAArray[]={3.5,4.0,3.8,3.1,2.9}; final String classInformationArray[]={"Peter Clay","Java Programming"}; final String stuInformationArray[]= new String [5];
//Ask for user input for (int i=0; i < 4;i ++) { do { //print out a menu System.out.println(); System.out.println("\n------- Student Management System ----------"); System.out.println("\n1. Student Information"); System.out.println("\n2. Class Information");
//convert string to integer choice = Integer.parseInt(choiceString);
switch (choice) { //the choices go here - print the details
case 1: for (int x=0; x<5; x++) { System.out.println("\nStudent Information: ["+ x +"]" + stuFullNameArray[x]); }break; case 2: for (int x=0; x<4; x++) { System.out.println("\nClass Enrollment Information: " + classInformationArray[x]); }break; case 3: for (int x=0; x < 5; x++) { System.out.println("\n" + stuFullNameArray[x] + "'s Graduation Year is: " + stuGradArray[x]); }break;
case 5: //exit System.exit(0); default: System.out.println("\nPlease choose a number between 1-5 only \n"); } } while(choice!= 5);
}//End of class statement } }
Thanks. I am getting further lost. I reread the instructions and i am way off base. "Once an user selects 1, they will see a list of students' info. If selecting 2, a user need to type in person's first name and/or last time, then the class registration information for the student will display. Student info and class info can be hard-coded within the Java classes. The key point I am looking from this assignment is how you call another Java class' method/variable from one Java class." Any additional help would be great.
Check out this program below. I took your code and made some edits to meet your teachers wishes. Here we create an array of Student objects. We load the students in by creating instances of the student class and pull the information out of your arrays to do this. We then load them into stuInformationArray.
We then proceed to ask the user for a choice and if they choose 1, it loops through the array and call each objects "printStudentInfo()" method. This is what your teachers was asking by calling another class' method.
Then in choice 2 we ask for a first or lastname and we loop through all the students again, looking for a match in the student's name. If there is a match, we print the student. Otherwise we tell them there was no students to be found.
//create an array variables to hold student information static int students=5; final static String stuFullNameArray[]={"Tom Tucker","Larry Fast","Martin Slow","Peter Fellows","Don Dinkin"}; final static String stuDOBArray[]={"March 11","Oct 10","Feb 7","Aug 17","May 19"}; final static Integer stuGradArray[]={2009,2010,2008,2010,2015}; final static String stuSSNArray[]={"111111111","222222222","333333333","444444444","555555555"}; final static Double stuGPAArray[]={3.5,4.0,3.8,3.1,2.9}; final static String classInformationArray[]={"Peter Clay","Java Programming"}; final static Student stuInformationArray[]= new Student [5];
public static void main(String[] args) throws IOException {
//create a bufferedReader object BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String choiceString; int choice;
//Ask for user input for (int i=0; i < 4;i ++) { do { //print out a menu System.out.println(); System.out.println("\n------- Student Management System ----------"); System.out.println("\n1. Student Information"); System.out.println("\n2. Class Enrollment Information"); System.out.println("\n3. Graduation Year"); System.out.println("\n4. Current G.P.A"); System.out.println("\n5. Quit"); System.out.println("\nEnter choice [1,2,3,4,5]: ");
System.out.flush(); choiceString = in.readLine();
//convert string to integer choice = Integer.parseInt(choiceString);
// Now load our students into the "Student" array. loadStudents();
switch (choice) { //the choices go here - print the details
// Here we loop through the Student objects and call their printStudentInfo() method. // This should meet your teachers goal of calling another class' function. case 1: for (int j = 0; j < stuInformationArray.length; j++) { stuInformationArray[j].printStudentInfo(); } break; case 2: boolean found = false;
// Here we prompt for a first or last name and search for it. We loop through the students // and if one has a name that matches part of the search string, it prints their info. // This again calls another method (getName) from our other student class. System.out.print("Please enter a first or lastname of student: " ); String studentsname = in.readLine();
for (int j = 0; j < stuInformationArray.length; j++) { if (stuInformationArray[j].getName().indexOf(studentsname) > -1) { stuInformationArray[j].printStudentInfo(); found = true; } }
if (!found) { System.out.println("Sorry, but that student doesn't exist in our system."); } break; case 3: break; case 4: break; case 5: //exit System.exit(0); default: System.out.println("\nPlease choose a number between 1-5 only \n"); } } while(choice!= 5); }//End of class statement }
// Load student objects into the stuInformationArray private static void loadStudents() { for (int i = 0; i < 5; i++) { stuInformationArray[i] = new Student(stuFullNameArray[i], stuDOBArray[i], stuSSNArray[i], stuGradArray[i], stuGPAArray[i]); } } }
// Here is our other class called "Student". It keeps track of a single student. class Student { private String stuName = ""; private String stuDateofBirth = ""; private String stuSocialSecurity = ""; private double stuGradePoint = 0.0; private int stuGradYear = 2008;
// We call this method to print the student's information. public void printStudentInfo() { System.out.println("Name: " + stuName); System.out.println("Date of Birth: " + stuDateofBirth); System.out.println("Graduation: " + stuGradYear); System.out.println("GPA: " + stuGradePoint); System.out.println(); }
// We use this method to get the student's name for searching against. public String getName() { return stuName; } }
Give it a whirl and read through the comments to see how this works. You can modify it for choices 3 and 4 but this should give you a good start and put you right back on the path to using multiple classes, calling that class' methods from the other class, and also provide you a mechanism for moving through students and managing them as objects.
Enjoy!
"At DIC we be student object handling code ninjas... Just don't give us first graders, they are too much for us."