1

I'm having trouble making option 7 work in my code (i.e. choice == 7), the one that outputs a student's set of quiz scores. When I run the program, it simply asks for jumps back to the original set of choices. Any help would be much appreciated.

package studentquizgrades;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;


public class StudentQuizGrades {

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);

    boolean finish = false;


    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, View a list of all students - 6");
        System.out.println("Get a student's quiz scores - 7, Quit - 8");
        int choice = userInput.nextInt();

        if (choice == 1) {
            Set<String> keySet = map.keySet();
            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) {
            Set<String> keySet = map.keySet();
            for (String currentKey : keySet) {
                Student student = map.get(currentKey);
                System.out.println();
                System.out.println(currentKey);
                System.out.println(Arrays.toString(student.getQuizGrades()));
                System.out.println();

            }

        } else if (choice == 3) {
            Set<String> keySet = map.keySet();
            int max = 0;
            String maxName = null;

            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];
                        maxName = currentKey;
                    }
                }
            }
            System.out.println();
            System.out.println("The highest quiz score was " + max + "; his/her name is " + maxName);
            System.out.println();

        } else if (choice == 4) {
            Set<String> keySet = map.keySet();
            int min = Integer.MAX_VALUE;
            String minName = null;

            for (String currentKey : keySet) {
                Student student = map.get(currentKey);
                int index = 0;
                int[] scores = student.getQuizGrades();

                for (int counter = 0; counter < scores.length; counter++) {
                    if (scores[counter] < min) {
                        minName = currentKey;
                        min = scores[counter];

                    }

                }

            }
            System.out.println();
            System.out.println("The lowest quiz score was " + min + "; his or her name is " + minName);
            System.out.println();

        } else if (choice == 5) {
            Set<String> keySet = map.keySet();
            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("");
            System.out.println("The class average is: " + average);
            System.out.println("");
        } else if (choice == 6) {
            Set<String> keySet = map.keySet();
            System.out.println("");
            System.out.println("List of students: ");
            for (String currentKey : keySet) {
                Student student = map.get(currentKey);

                System.out.println(currentKey);

            }
        } 
        else if(choice == 7){


            Set<String> keySet = map.keySet();
            System.out.println("");
            System.out.println("Please enter a student's name: ");
            String studentName = userInput.nextLine();
            for (String currentKey : keySet) {

                Student student = map.get(currentKey);

                if(studentName == currentKey){

                    System.out.println(currentKey + "'s quiz scores:");
                    int [] quizArray = student.getQuizGrades();
                    for(int counter1 = 0; counter1 < quizArray.length; counter1++ ){

                        System.out.println(quizArray[counter1]);

                    }
                }

            }
        }


        else if (choice == 8) {
            finish = true;
            break;
        }
    } while (finish == false);
}
}

package studentquizgrades;

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;
    }

}

statsguyz
  • 323
  • 2
  • 8
  • 24
  • 1
    Well you can't compare strings using == like this: `if(studentName == currentKey){` Gotta use the 'equals' method: `string.equals( otherString );` – markspace Dec 18 '16 at 23:39
  • Ah ok, I knew something was wrong. Thanks! – statsguyz Dec 18 '16 at 23:40
  • After selecting 7, does it let you enter the student's name? What is the exact output? – jbunting Dec 18 '16 at 23:41
  • 1
    Also, that loop looks wrong. You should be able to just use `map.get( studentName );` but I didn't analyze the code carefully. – markspace Dec 18 '16 at 23:42
  • Possible duplicate of [Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods](http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo) – Code-Apprentice Dec 19 '16 at 01:00

2 Answers2

3

You mix scanner.nextInt() and scanner.nextLine(). When you call scanner.nextInt() and then you call scanner.nextLine(), scanner.nextLine() returns the remain of the current line of the input entered by scanner.nextInt(), that is an empty String. And that before you may enter anything.
That's why you loop to the original set of choices.

You should avoid to chainnext() and nextLine(). Try to use only the one or the other one.

Besides, the code in the else if(choice == 7) block compares String values with == and besides this comparison is even not required since you perform a loop on the map of students to retrieve the student while you have a Map that may retrieve the information in a straight way :

Set<String> keySet = map.keySet();
System.out.println("");
System.out.println("Please enter a student's name: ");
String studentName = userInput.next();
for (String currentKey : keySet) {

     Student student = map.get(currentKey);

     if(studentName == currentKey){

             System.out.println(currentKey + "'s quiz scores:");
             int [] quizArray = student.getQuizGrades();
              for(int counter1 = 0; counter1 < quizArray.length; counter1++ ){
                      System.out.println(quizArray[counter1]);
               }
      }

}

You could do simply :

System.out.println("");
System.out.println("Please enter a student's name: ");
String studentName = userInput.next();

Student student = map.get(studentName);
if (student!=null){
      System.out.println(studentName + "'s quiz scores:");
      int[] quizArray = student.getQuizGrades();
      for (int counter1 = 0; counter1 < quizArray.length; counter1++) {
         System.out.println(quizArray[counter1]);
      }
 }
davidxxx
  • 104,693
  • 13
  • 159
  • 179
2

Instead of if (studentName == currentKey) use if (studentName.equals(currentKey). You should always compare strings with equals(...), because == only checks the equality of references, not the objects itself. As studentName and current key are two independent instances, they are never equal in a comaprison with == as their references differ.

You should try to refactor your code, as there are multiple issues. As @markspace already pointed out, your loop is unnecessary and the critical if statement is not needed at all.

for (String currentKey : keySet) {
    Student student = map.get(currentKey);
    if(studentName == currentKey){
        //...
    }
}

This can be reduced to the following code as you deal with a map. That is the point of a map, not having to iterate through all elements, but to use a key to directly access an element.

Student student = map.get(currentKey);
if (student != null) {
    // ...
}

Also, you should call nextLine(); after int choice = userInput.nextInt(); (and other next...(...); calls), because they do not consume the newline character. Otherwise studentName = userInput.nextLine(); will only contain the newline character leftover from last call to nextInt()...;.

Community
  • 1
  • 1
thatguy
  • 13,242
  • 6
  • 19
  • 33