0

I am making a program where you enter in the student's grade, and it counts who passes and fails, then Z is supposed to mean the program should end. But whenever I type in "Z", it just prints out:

<Z is NOT an acceptable grade.>

So something is preventing the while loop from realizing I am trying to move on. Any ideas? Full code is below.

import java.lang.reflect.Array;

import java.util.Scanner;
import java.util.Arrays;
import java.util.List;

public class GCDProgram {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int numberOfGrades = 1;
System.out.println("Enter grade #" + numberOfGrades + ":");
String inputLetter = sc.nextLine();

int passed = 0;
int failed = 0;

String[] letterGrade = {"A", "B", "C", "D", "F"};
boolean result = Arrays.stream(letterGrade).anyMatch(inputLetter::equals);



while (inputLetter != "Z"){


if (result) {

    numberOfGrades++;
    System.out.println("Enter grade #" + numberOfGrades + ":");
    inputLetter = sc.nextLine();
    result = Arrays.stream(letterGrade).anyMatch(inputLetter::equals);
    if ((inputLetter == "D") || (inputLetter == "F")){
        failed++;
    }else{
        passed++;
    }
}else{
    System.out.println("<" + inputLetter + " is NOT an acceptable grade.>"); 
    System.out.println("Enter grade #" + numberOfGrades + ":");
    inputLetter = sc.nextLine();
    result = Arrays.stream(letterGrade).anyMatch(inputLetter::equals);
}
}
System.out.format("%-20s %3s", numberOfGrades + " students total", "");
System.out.format("%-20s %3s", passed + " students passed:", ((passed / numberOfGrades) * 100) + "%");
System.out.format("%-20s %3s", failed + " students failed:", ((failed / numberOfGrades) * 100) + "%");
 }


}
Talha Awan
  • 4,573
  • 4
  • 25
  • 40
Andy Lebowitz
  • 1,471
  • 2
  • 16
  • 23

1 Answers1

1

Please try with this condition:

while (!"Z".equals(inputLetter)){
Martinus_
  • 160
  • 1
  • 3
  • 13