简体   繁体   中英

How to pass a string array or integer value from a class to another using java?

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM