Question

i have trouble with my java project homework. in this program when i use case 1 it activates student recording system. i need i must write all of this recording system with method. i tried but i failed. can you show me how can i write method which records informations to array ? Thanks a lot .

//===========================================values
String[] name = new String [100];
String[] gender = new String [100];
String[] studentNo = new String [100];
Double[] gpa = new Double [100];
int menu = 0;
int accountNumber = 0;

//===========================================
menu = in.nextInt();
switch (menu) 
case 1:  {
System.out.println("** Recording a new student");
System.out.println("*** Please use lower case");
in.nextLine(); // for solve skipping 

System.out.print("Enter Student Name and Surname: ");
name[accountNumber] = in.nextLine();

System.out.print("Enter Student Gender(m/f): ");
gender[accountNumber] = in.nextLine();

System.out.print("Enter Student Number: ");
studentNo[accountNumber] = in.nextLine();

System.out.print("Enter Student GPA: ");
gpa[accountNumber] = in.nextDouble();

accountNumber++;


System.out.println("New Student Recorded. There are ["+accountNumber+"] students in system.");
System.out.println("");
}break;
Était-ce utile?

La solution

Well, I agree with people to do your homework, to use methods you just have to know, you must separate code, "divide and conquer", now that you submitted your project, the solution could be this way (using arrays of course), I would have used a bean or dto to store the info, but maybe is a requirement for you to use arrays:

public class DoYourHomework {

    String[] name = new String[100];
    String[] gender = new String[100];
    String[] studentNo = new String[100];
    Double[] gpa = new Double[100];
    int menu = 0;
    int accountNumber = 0;

    public DoYourHomework() {
        Scanner in = new Scanner(System.in);
        menu = in.nextInt();
        switch (menu) {
        case 1:  {
        System.out.println("** Recording a new student");
        System.out.println("*** Please use lower case");
        in.nextLine(); // for solve skipping 

        String name = null;
        String number = null;
        String gender = null;
        double gpa = -1;

        System.out.print("Enter Student Name and Surname: ");
        name = in.nextLine();

        System.out.print("Enter Student Gender(m/f): ");
        gender = in.nextLine();

        System.out.print("Enter Student Number: ");
        number = in.nextLine();

        System.out.print("Enter Student GPA: ");
        gpa = in.nextDouble();

        registerNewStudent(name, gender, number, gpa);
        accountNumber++;


        System.out.println("New Student Recorded. There are ["+accountNumber+"] students in system.");
        System.out.println("");
        }
        }

    }

    private void registerNewStudent(String name2, String gender2,
            String number, double gpa2) {
        name[accountNumber] = name2;
        gender[accountNumber] = gender2;
        studentNo[accountNumber] = number;
        gpa[accountNumber] = gpa2;
    }

    public static void main(String[] args) {
        new DoYourHomework();
    }

}

Hope it helps you to understand for your next exercises. Maybe, as a plus, you can add validation to avoid the same student number or try to do the next operations such "modify and delete" by yourself. Best regards.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top