Reputation: 3
I'm relatively new to Java, currently studying it in a night course. I'm having trouble setting the constructor up correctly so that it moves values entered into an array. I have the below code and it seems that the individual fields are being passed correctly but the array is not populated and I can't figure out what I'm doing wrong. I've gone back through my notes, textbooks and checked here. I may just have been staring at it for too long!
import java.util.Scanner;
public class Main {
static Scanner InOut = new Scanner(System.in);
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
//* Declare and initalize variables. Set maximum number of students to 6.
String surname = "", forename = "", course = "a", newCourse, tmpStu ="";
int firstMark = 0, secondMark = 0, thirdMark = 0, finalScore = 0,
option = 0, studentCount = 0, delNum, index = 0;
final int MAXSTUDENT = 6;
Student[] myClass = new Student[MAXSTUDENT];
//* Set class details for the constructor.
while (option != 6) {
//* Call the menu system to be displayed
option = menu();
switch (option) {
//* Checks the value return by the menu for the selected option
case 1:
//* Checks that the maximum number of students has not been exceeded.
if (studentCount >= MAXSTUDENT) {
System.out.println("Maximum number of student entered. \n\n");
menu();
} else {
//* Enter in the student details
InOut.nextLine(); /* ensures that the buffer is cleared*/
System.out.println("Enter student surname: ");
surname = InOut.nextLine();
System.out.println("Enter student forename: ");
forename = InOut.nextLine();
System.out.println("Enter 1st mark: ");
firstMark = InOut.nextInt();
System.out.println("Enter 2nd mark: ");
secondMark = InOut.nextInt();
System.out.println("Enter 3rd mark: ");
thirdMark = InOut.nextInt();
finalScore = 0;
//* Passes the entered paramaters to add the student details to the object class.
//* Increases the student counter by 1.
myClass[studentCount] = new Student(surname, forename, course, firstMark, secondMark, thirdMark, finalScore);
System.out.println(myClass[studentCount]);
studentCount++;
}
break;
case 2:
//* Enter in the student number to be deleted. Number is passed for deletion.
//* Reduces the number of student counter.
System.out.println("Delete which student?");
delNum = InOut.nextInt();
deleteStudent(delNum, studentCount);
studentCount--;
break;
case 3:
//* Allows for the course name to be updated.
InOut.nextLine(); /* ensures that the buffer is cleared*/
System.out.println("Enter new course: ");
newCourse = InOut.nextLine();
Student.setCourse(newCourse);
break;
case 4:
//* Display all details stored in the object class.
for (index = 0; index < studentCount; index = index + 1){
Student.displayDetails(index);
}
break;
case 5:
//* Input the surname of student and this name is passed to
//* display the details for this student only.
InOut.nextLine(); /* ensures that the buffer is cleared*/
System.out.println("Enter surname of student you want to search for: ");
String searchName = InOut.nextLine();
//* searchStudent(myClass, searchName, index, studentCount);
break;
case 6:
//* Exit program
System.out.println("End of program");
break;
default:
System.out.println("Invalid option");
}
}
}
public static int menu() {
//* Display menu options and returns selection.
System.out.println("****Student Menu****");
System.out.println("1. Add new student details");
System.out.println("2. Delete Student");
System.out.println("3. Change Course");
System.out.println("4. Display All Student Details");
System.out.println("5. Search Student by Name");
System.out.println("6. Exit");
System.out.println("\nInput an option:");
int option = InOut.nextInt();
return option;
}
public void addStudent(String surname, String forename, String course, int firstMark, int secondMark, int thirdMark, int finalScore)
//* Accepts passed paramaters of student details and adds them into the object class.
{
System.out.println(surname);
System.out.println(forename);
System.out.println(course);
System.out.println(firstMark);
System.out.println(secondMark);
System.out.println(thirdMark);
System.out.println(finalScore);
}
public static void deleteStudent(int delNum, int studentCount)
//* Accepts student array number to be deleted and displays details of all
//* students still stored.
{
//* int asize = myClass.length, index;
//* for (index = delNum +1; index < asize; index++)
//* {
//* myClass[index-1] = myClass[index];
//* }
//* studentCount --;
}
And this is the Student Constructor being called. I'd added in a few display statements to see if I could find where it was going wrong but no luck. I think it may be to do with the constructor being called from the "static void" main method.
import java.util.Scanner;
public class Student {
private String surname, forename;
private int firstMark, secondMark, thirdMark, finalScore, index;
private int stuCount = 0;
private final int NUMMARK = 3;
private static String course = "French";
Scanner InOut = new Scanner(System.in);
public Student(String surname, String forename, String course, int firstMark, int secondMark, int thirdMark, int finalScore)
{
System.out.println(surname);
this.surname = surname;
this.forename = forename;
this.course = course;
this.firstMark = firstMark;
this.secondMark = secondMark;
this.thirdMark = thirdMark;
finalScore = ((firstMark + secondMark + thirdMark)/ NUMMARK);
this.finalScore = finalScore;
}
public static void setCourse (String newCourse) {
course = newCourse;
}
private String getDetails()
//* Builds student details and returns them to be displayed.
{
String details = "Student Name: " + surname + ", " + forename +"\n";
details = details + "Course Name: " + course + "\n";
details = details + "Student's Marks: " + firstMark + ", " + secondMark + ", " + thirdMark + "\n";
details = details + "Final Mark: " + finalScore + "\n";
return details;
}
public static void displayDetails(int index)
//* Displays all student details that have been entered using the student counter
//* for the number of times to loop.
{
//* if (myClass[index] != null) {
//* System.out.println(myClass[index].getDetails());
//* }
}
}
Upvotes: 0
Views: 205
Reputation: 11
your code doesn't have any serious problem. you just have to edit some part of it as follow:
first of all to see the getDeatails() method of your Student class you should declare it as protected: so the new version is as follow
protected String getDetails()
{
String details = "Student Name: " + surname + ", " + forename + "\n";
details = details + "Course Name: " + course + "\n";
details = details + "Student's Marks: " + firstMark + ", " + secondMark + ", " + thirdMark + "\n";
details = details + "Final Mark: " + finalScore + "\n";
return details;
}
in the first case what you write is ok. you just have to use the appropriate way to retrieve the data from an array. so use the following code:
//* Passes the entered paramaters to add the student details to the object class.
//* Increases the student counter by 1.
myClass[studentCount] = new Student(surname, forename, course, firstMark, secondMark, thirdMark, finalScore);
for(int i = 0;i<=studentCount;i++){
System.out.println(myClass[i].getDetails());
System.out.println("-----------------------");
}
studentCount++;
System.out.println("now the student count are: "+studentCount);
the following is for case 4:
case 4:
//* Display all details stored in the object class.
System.out.println("studentcount:"+studentCount);
for (index = 0; index < studentCount; index = index + 1) {
System.out.println(myClass[index].getDetails());
System.out.println("---------------------");
}
break;
Upvotes: 1
Reputation: 22243
This
myClass[0] = new Student(surname, forename, course, firstMark, secondMark, thirdMark, finalScore);
studentCount++;
System.out.println(myClass[0]);
Is adding each new student on the previous one, you are always setting the first position of the array, it should be like this:
myClass[studentCount] = new Student(surname, forename, course, firstMark, secondMark, thirdMark, finalScore);
System.out.println(myClass[studentCount]);//BEFORE the counter increment
studentCount++;
Side note:
This code:
System.out.println(myClass[studentCount]);
Will not print the informations that the student contains. In order to print them you have to override the toString
method of your Student
class, returning the string you want to be printed
Upvotes: 3