0

I created a simple console based student system where basically the program will ask if you want to input name of students or view the list of names you inputted. The flow of the output is as shown below:

*******CONSOLE BASED STUDENT SYSTEM************
1. Input Student Name
2. List of students
Enter the number of choice: 1
Enter number of students to input: 3
****************************************
Student No. 1
Enter full name: Alpha Jones
****************************************
Student No. 2
Enter full name: Beta Jones
****************************************
Student No. 3
Enter full name: Gamma Jones
Do you wish to proceed back to menu?(y/n): y

*******CONSOLE BASED STUDENT SYSTEM************
1. Input Student Name
2. List of students
Enter the number of choice: 2
******LIST OF STUDENTS******
NULL
NULL
NULL

Firstly, I input three new students names Alpha Jones, Beta Jones and Gamma Jones but when I chose to view all names, everything is null. The three names should appear.

Here is the code for Student Class:

public class Student {
private String[] names;
private int numInput;

public Student(){

}

public Student(String[] fullName, int nI){
    numInput = nI;
    names = new String[nI];
    for(int index = 0; index < nI; index++){
        names[index] = fullName[index];
    }
}

public String[] getList(){
    return names;
}

public int getNumStudents(){
    return numInput;
}
}

This is where I setup the values that will be passed on from the PracticeOne class, and later on, I will return that value back for display in PracticeOne Class.

Here is the PracticeOne Class(This is where the main method is located):

import java.util.Scanner;
public class PracticeOne {
public static void main(String[]args){
    Scanner hold = new Scanner(System.in);
    int menuNumChoice;

    String response;
    do{

        System.out.println("******CONSOLE BASED STUDENT SYSTEM******");
        System.out.println("1. Input Student Name");
        System.out.println("2. List of Students");
        System.out.print("Enter the number of choice: ");
        menuNumChoice = hold.nextInt();

        switch(menuNumChoice){
            case 1:
                inputStudentName(hold);
                break;
            case 2:
                listStudents();
                break;
            default:
                System.out.println("ERROR 101");
                break;
        }

        System.out.print("Do you wish to proceed?(y/n): ");
        response = hold.nextLine();

    }while(response.equalsIgnoreCase("y"));
}

public static void inputStudentName(Scanner hold){
    int numInput;


    System.out.print("Enter number of students to input: ");
    numInput = hold.nextInt();

    hold.nextLine();
    String[] fullName = new String[numInput];

    for(int x = 0; x < numInput; x++){

        System.out.println("***************************");
        System.out.println("Student No. " + (x + 1));
        System.out.print("Enter full name: ");
        fullName[x] = hold.nextLine();
        System.out.println();

    }

    Student s = new Student(fullName,numInput);

}

public static void listStudents(){
        Student s = new Student();
        System.out.println("******LIST OF STUDENTS******");
        for(int y = 0; y < s.getNumStudents();y++){
            System.out.println(s.getList());
        }

}
}

Firstly I called an instance of Student in inputStudentName() method, which passes an argument for the fullname and the number of arrays being used, and tries to transfer it to Student class constructor, to get the values from there later on.

In the listStudents method, I tried displaying it by calling the getList() method from Student class to supposedly get back the names that was inputted earlier but everything was NULL. I also tried getting back the number of arrays through the getNumStudents() method but it also failed.

Kindly share some advice on how to work around with this problem or if theres a better way, suggest new things to achieve my goal.

2
  • You create a new Student in inputStudentName but never reurn it... Commented Dec 11, 2019 at 9:00
  • @Ward it doesn't have to be returned, it should be stored. If you have it return it, any previously read Student data would be overridden Commented Dec 11, 2019 at 9:09

2 Answers 2

1
public static void listStudents(){
        **Student s = new Student();**
        System.out.println("******LIST OF STUDENTS******");
        for(int y = 0; y < s.getNumStudents();y++){
            System.out.println(s.getList());
        }

}

This is your listStudents logic. Instead of using the data you already created, you just create a new instance of Student. It's quite normal that this doesn't contain any information.

In your input method, you also only save the data in a local variable. This means, that once the method is finished, the data is lost.

Add a static List<Student> students = new ArrayList<>(); to your class. At the end of each input method, add the Student object to this list. In listStudents, don't create a local student, but print the ones you stored in this List.

Sign up to request clarification or add additional context in comments.

2 Comments

The student class is a little bit off too. He is storing several names in a single student instance and is overriding the numInput. Just storing his students in a List will probably not solve his problem.
@JuliusHörger actually, it will. It will just create a logic in the code that is ... not logical, but that issue is already there. Once he stores his Students in a list, in hist listStudents he'll need to first iterate the List, and then for each Student instance perform the print he has now. It will be messy code and should be refactored, but when proposing a minimal of changes, it's more likely that the OP understands them.
0

Based on your current system,first you can create a global variable Student[] or List then at the end of ' input Student Name()' method to save your dat. Another Way you can use database to save your data,But this is not necessary for your system.

1 Comment

Java doesn't support global variables. Creating an array of type Student[] could be dangerous, since either you need to know up front the number of students you'll read, or you need to create a new array each time you exceed it's limit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.