简体   繁体   中英

Why won't my array pass to the other class?

I have looked at similar examples or other programs that call array from another class and it seems like I have done it the correct way but I am still getting errors. This is where the arrarys are stored:

import java.util.Scanner;
public class DriverProgram {
    public static int[] IDs = new int[10];
    public static String[] names = new String[10];
    public static double[] grades = new double[10];
    public static int i = 0;
    static Student call = new Student();
    public static void main(String[] args){
        call = new Student();
        Scanner command = new Scanner(System.in);
        System.out.println("Please Enter a command(i, r, s, or d): ");
        while(command.hasNext()){
            char command1 = command.next().charAt(0);
            if(command1 == 'i'){
                call.AddToArray(IDs[], names[] , grades[], i);
            }else if(command1 == 'r'){
                call.RemoveFromArray(int [] IDs, String [] names,double [] grades, int i);
            }else if(command1 == 's'){
                call.SortArray(int [] IDs, String [] names,double [] grades, int i);
            }else if(command1 == 'd'){
                call.DisplayArray(int [] IDs, String [] names,double [] grades, int i);
            }else if(command1 == 'z') {
            break;
            }
            else System.out.println("Invalid command enter a valid command next time.");
            System.out.println("Please Enter a command(i, r, s, or d) or z to finish: ");
        }
    }

And this is what I am tryign to call the arrays to:

import java.util.Scanner;
public class Student {

    public static void AddToArray(int[] IDs, String[] names, double[] grades, int i) {

        if (i >= 10) {
            System.out.println("You have already inputted 10 students please delete one first.");
        } else {
            Scanner readin = new Scanner(System.in);
            Scanner readinname = new Scanner(System.in);
            Scanner readingrade = new Scanner(System.in);
            System.out.println("Please enter student ID: ");
            IDs[i] = readin.nextInt();
            System.out.println("Please enter student name: ");
            names[i] = readinname.nextLine();
            System.out.println("Please enter student grade: ");
            grades[i] = readingrade.nextDouble();
            System.out.println(IDs[i] + " " + names[i] + " " + grades[i]);
            i++;
            for (int j = 0; j < i; j++) {
                if (IDs[j] == IDs[i]) {
                    System.out.println("This student has already been entered.");
                }else{
                    System.out.println("The student has been added");
                    break;
                }

            }
        }
    }

I am not sure what else I need or what I am missing in order to call those arrays.

call.AddToArray(IDs[], names[] , grades[], i);

should be replaced with

call.AddToArray(IDs, names , grades, i);

PS Design notes

  • Student has only static method, so this is utilitly class and should not allowed an instance creation
  • call.AddToArray() and others static methods should be called as Student.AddToArray()
  • array is not correct data strucutre where you can add or remove elements. There're more suitable data structures like List or Map .
  • It's better to use only one instance of Scanner .

This is how you DriverProgram could look like.

public class DriverProgram {

    public static void main(String[] args) {
        Map<Integer, Student> students = new HashMap<>();
        Scanner scan = new Scanner(System.in);

        while (scan.hasNext()) {
            System.out.println("Please Enter a command [1-5]:");
            System.out.println("1. add new student");
            System.out.println("2. remove existed student");
            System.out.println("3. sort existed students by grades desc");
            System.out.println("4. show existed students");
            System.out.println("5. exit");
            System.out.print("> ");

            int menu = scan.nextInt();

            if (menu == 1)
                addStudent(scan, students);
            else if (menu == 2)
                removeStudent(scan, students);
            else if (menu == 3)
                sortStudents(students);
            else if (menu == 4)
                showStudents(students);
            else if (menu == 5)
                break;

            System.err.println("Unknown command. Try again");
        }
    }

    private static void addStudent(Scanner scan, Map<Integer, Student> students) {
        if (students.size() == 10) {
            System.err.println("You have already inputted 10 students please delete one first.");
            return;
        }

        System.out.print("Please enter student ID: ");
        int id = scan.nextInt();

        if (students.containsKey(id)) {
            System.err.println("This student with this id has already been entered.");
            return;
        }

        System.out.print("Please enter student name: ");
        String name = scan.nextLine();

        System.out.print("Please enter student grade: ");
        double grade = scan.nextDouble();

        students.put(id, new Student(id, name, grade));
    }

    private static void removeStudent(Scanner scan, Map<Integer, Student> students) {
    }

    private static void sortStudents(Map<Integer, Student> students) {
    }

    private static void showStudents(Map<Integer, Student> students) {
    }

    public static final class Student {

        private final int id;
        private final String name;
        private final double grade;

        public Student(int id, String name, double grade) {
            this.id = id;
            this.name = name;
            this.grade = grade;
        }

    }

}

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