Hye guys can u help me with this code my addMarks method doesnt work

import java.util.Objects;
import java.util.Scanner;
import java.util.Arrays;
public class Project {
//I’ve put the attributes of the arrays outside the main methods because the methods that I’ve made are not static
String names= {“Nadim”, “Sarah”,“Alfred”,“Hala”,“Abed”};
int marks= {
{90, 45, 67, 23, 35},
{78, 82, 78, 83, 55},
{79, 83, 86, 89, 99},
{80, 84, 87, 90, 90},
{81, 85, 88, 91, 90},
{45,67,89,100,23,42}
};
public static void main(String args) {
Project Class = new Project();
Scanner scan = new Scanner(System.in);

    System.out.println("""
                Available choices
                (add) To add a new student
                (remove) To remove a Student
                (gpa) To get a student GPA
                (highest gpa) To get the student with highest GPA
                (lowest gpa) To get the student with lowest GPA
                (quit) To Exit
                """);

    String operation;

    do{
        System.out.print("Enter the name of the operation to begin: ");

        operation = scan.nextLine();
        //this is teh main switch statement that controls all the methods below
        switch (operation) {
            case "gpa" -> {
                System.out.print("Enter the name of the student: ");
                String sum = scan.nextLine();
                Class.getSum(sum);
            }
            case "add" -> {
                System.out.print("Enter the name of the student: ");
                String add = scan.nextLine();
                Class.addStudent(add);
                System.out.print("Now enter the student's grades: ");
                Class.addMarks();
            }
            case "remove" -> {
                System.out.print("Enter the name of the student: ");
                String remove = scan.nextLine();
                Class.removeStudent(remove);
            }
            case "highest gpa" -> Class.highestGpa();
            case "lowest gpa" -> Class.lowestGpa();
        }

    }while (!Objects.equals(operation, "quit"));
}
public void getSum(String nameSum) {
    int sum= 0;
    //this loop is pretty simple, and I've used it many times, and it basically sums the marks of the chosen student to then get divided by the length of the array to give us the gpa
    for (int i= 0; i <names.length; i++) {
        if (Objects.equals(nameSum, names[i])) {
                for (int j =0; j<marks[i].length; j++) {
                    sum += marks[i][j];
                }
            }
        }
    int avg = (sum/5);
    System.out.println("The student's GPA is " + avg);
}

public void addStudent(String nameAdd) {
    //in the line below I made a new array that is one longer by one value to hold the new value of the added student name
    String[] moreNames= new String[names.length + 1];
    //this loop copies the values of the old array to the new one
    for (int i=0; i <names.length; i++) {
        moreNames[i] = names[i];
    }
    //here we add the new name that is chosen by the user
    moreNames[moreNames.length -1] = nameAdd;
    names= moreNames;
    System.out.println(Arrays.toString(moreNames));
}
public void addMarks() {
    int[][] newMarks = new int[marks.length+1][];

    for (int e=0; e<marks.length; e++) {
        for (int p =0; p < marks[e].length; p++) {
            newMarks[e][p] = marks[e][p];
        }
    }

    for (int k=0; k < marks[marks.length].length; k++) {
        Scanner scan = new Scanner(System.in);
            newMarks[marks.length][k] = scan.nextInt();
    }
    System.out.println(Arrays.toString(newMarks[marks.length]));
}
public void highestGpa() {
    String highStudent= "";

    int y = -1;
    int sum;

    //in the line below I made a new array to hold the values that the loop will create from teh old array
    int[] newMarks= new int[marks.length];

    //the loop will make new values to then ut in a new array that hold 5 summations of the old secondary arrays
    for (int[] mark : marks) {
        y++;
        sum = 0;
        for (int i : mark) {
            sum += i;
        }
        newMarks[y] = sum;
    }

    int oldGpa =newMarks[0];
    //in this loop I can find the highest gpa of the students
    for (int z=0; z< newMarks.length; z++) {
            if (newMarks[z] > oldGpa) {
                oldGpa = newMarks[z];
                highStudent = names[z];
        }
    }
    double newGpa =oldGpa/5;
    System.out.println("The Highest gpa is " + newGpa + " and the name of the student who got it is " + highStudent + "\n");
}
public void lowestGpa() {
    String lowStudent= "";

    int y = -1;
    int sum;

    //in the line below I made a new array to hold the values that the loop will create from teh old array
    int[] newMarks= new int[marks.length];

    //the loop will make new values to then put in a new array that hold 5 summations of the old secondary arrays
    for (int[] mark : marks) {
        y++;
        sum = 0;
        for (int i : mark) {
            sum += i;
        }
        newMarks[y] = sum;
    }

    int newSum =newMarks[0];
    int z;

    //in this loop I can find the lowest gpa of teh students
    for (z=0; z< newMarks.length; z++) {
        if (newMarks[z] < newSum) {
            newSum = newMarks[z];
        }
        lowStudent = names[z];
    }
    double newGpa =newSum/5;
    System.out.println("The lowest gpa is " + newGpa + " and the name of the student who got it is " + lowStudent + "\n");
}
public void removeStudent(String nameToRemove) {

    //making a new shorter array than teh names array, so we can save the results to it
    String[] newNames = new String[names.length-1];
    int indexLess=0;

    //in the lines below are put to copy the names don't math the one selected of the old array to the new one
    for (String name : names) {
        if (!Objects.equals(name, nameToRemove)) {
            newNames[indexLess] = name;
            indexLess++;
        }
    }

    //making the names array equal to the newNames so when we use the program again the last results will be saved
    names = newNames;
    System.out.println("The new lis tof student names is " + Arrays.toString(newNames));
}

}

In the addMarks method,
you created a new 2D array called newMarks,
and then copied everything in marks to newMarks
and then used user input to update newMarks.
But there is nothing that updates or changes marks.

You can use
marks = newMarks;
to change the array stored in the variable marks to be the new 2D array from newMarks;

your code

Also, if you plan on having more than one instance of Project, but you want all those instances (all Project objects) to share the same marks and names, then you’ll need to make those variables static.

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.