Finding key within Map

50 Views Asked by At

I'm trying to print out the name of the student with the lowest quiz score in my program.

Unfortunately I'm not having much luck figuring out a way to use the getKey() method to identify the student's name. The issue I am having is within the statement:

else if (choice == 4)

Any help would be much appreciated.

public class StudentQuizGrades {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    Map<String, Student> map = new HashMap<>();

    addStudent(map);

}

private static void addStudent(Map<String, Student> map) {
    Scanner userInput = new Scanner(System.in);
    Set<String> keySet = map.keySet();
    boolean finish = false;
    int studentID = 0;

    do {
        System.out.println("Please choose an option: ");
        System.out.println("Add student and quizzes - 1, Get all quiz scores - 2, Get highest quiz score- 3, ");
        System.out.println("Get lowest quiz score - 4, Get class average - 5, Quit - 6");
        int choice = userInput.nextInt();

        if (choice == 1) {
            System.out.println("How many students would you like to add?");
            int numberOfStudents = userInput.nextInt();
            for (int counter = 0; counter < numberOfStudents; counter++) {

                System.out.println("ENTER NAME");
                Scanner addName = new Scanner(System.in);
                String name = (addName.nextLine());

                System.out.println("Enter First Quiz Score");
                Scanner addQuiz1 = new Scanner(System.in);
                int quiz1 = (addQuiz1.nextInt());

                System.out.println("Enter Second Quiz Score");
                Scanner addQuiz2 = new Scanner(System.in);
                int quiz2 = (addQuiz2.nextInt());

                System.out.println("Enter Third Quiz Score");
                Scanner addQuiz3 = new Scanner(System.in);
                int quiz3 = (addQuiz3.nextInt());
                Student student = new Student(name, quiz1, quiz2, quiz3);
                map.put(student.getKey(), student);

            }

        } else if (choice == 2) {

            for (String currentKey : keySet) {
                Student student = map.get(currentKey);
                System.out.println(currentKey);
                System.out.println(Arrays.toString(student.getQuizGrades()));

            }

        } else if (choice == 3) {

            int max = 0;
            for (String currentKey : keySet) {
                Student student = map.get(currentKey);
                int[] scores = student.getQuizGrades();
                for (int counter = 1; counter < scores.length; counter++) {
                    if (scores[counter] > max) {
                        max = scores[counter];
                    }
                }
            }
            System.out.println("The highest quiz score was " + max);


        } else if (choice == 4) {

            int min = 0;

            for (String currentKey : keySet) {
                Student student = map.get(currentKey);

                int[] scores = student.getQuizGrades();
                int index = 0;
                min = scores[0];

                for (int counter = 1; counter < scores.length; counter++) {
                    if (scores[counter] < min) {
                        min = scores[counter];
                        index = counter;


                    }


                } 
                studentID = index;
            } 


            System.out.println("The lowest quiz score was " + min );

        } else if (choice == 5) {

            int[] allGrades;
            int sum = 0;
            int counter2 = 0;
            for (String currentKey : keySet) {
                Student student = map.get(currentKey);
                int[] scores = student.getQuizGrades();
                for (int counter = 0; counter < scores.length; counter++) {
                    int j = scores[counter];
                    sum = sum + j;
                    counter2++;
                }

            }
            int average = sum / counter2;
            System.out.println("The class average is: " + average);

        } else if (choice == 6) {
            finish = true;
            break;
        }

    } while (finish == false);
}
 }


public class Student {
private String key;
private int grade1;
private int grade2;
private int grade3;

    public Student(String key, int grade1, int grade2, int grade3){
        this.key = key;
        this.grade1 = grade1;
        this.grade2 = grade2;
        this.grade3 = grade3;
    }

    public String getKey(){
        return key;
    }

    public int[] getQuizGrades(){
       int [] anArray = {grade1, grade2, grade3};
       return anArray;
    }

    public int getAverageScore(){
        int average = (grade1 + grade2 + grade3)/3;
        return average;
    }
}
2

There are 2 best solutions below

0
On BEST ANSWER

you can use a String Type to store the name of lowest. And maybe your code exist other small flaws.

an alternative as below:

else if (choice == 4) {
        Set<String> keySet = map.keySet(); //you should get keySet at here rather as your code for it maybe out date after 1 adding 
        int min = Integer.MAX_VALUE;// 
        String minName = null;///used to store the name of lowest
        for (String currentKey : keySet) {
            Student student = map.get(currentKey);
            int[] scores = student.getQuizGrades();

            for (int counter = 0; counter < scores.length; counter++) {//begin with 0 not 1 as your code
                if (scores[counter] < min) {
                    minName = currentKey;//store name
                    min = scores[counter];
                }
            } 
        } 
        System.out.println("The lowest quiz score was " + min + "his/her name is" + minName);
    }
0
On

Each student has three scores, and you want to find the student whose average (or minimum) of the three scores is the lowest among all students, right?

But your code currently search for the lowest among the three scores for each student, and you loop through all students, so you always get the lowest score of the LAST student in the collection keySet.