-3

For some reason, my program stops when it reaches the part that asks the user if it know it's exam 1 score. I need the user to be able to enter yes or no. Why does the program stop? I need it to work properly. I have all the if-else statements. I am able to enter the percentage weights, but that is all that the program will do. More must be done. My code extends far beyond entering the percentage weights. Please help me.

import java.util.Scanner;
public class GradeCalculation {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Scanner grade = new Scanner (System.in);

        // A new scanner must be created. The scanner is essential to this program performing properly. 

        double A = 90-100; 
        double B = 80-89; 
        double C = 70-79; 
        double D = 60-69; 
        double F = 0-59;
        String LetterGrade; 
        String yes; 
        String no;
        double Exam1, Exam2, finalExam, Labs, Projects, Attendance, Quizzes;
        double Exam1Grade, Exam2Grade, finalExamGrade, LabAverage, ProjectsAverage, AttendanceAverage, QuizzesAverage;
        double knownWeight;
        double PercentageWeights;

        // As always, the variables must be declared at the beginning of the program. 


        System.out.print(
                "Grading Scale:\n"+
                "A = 90-100 \n"+
                "B = 80-89 \n"+ 
                "C = 70-79 \n"+ 
                "D = 60-69 \n"+
                "F = 00-59 \n");

        System.out.println("What letter grade do you want to achieve in this course?");
        LetterGrade = grade.next();

        // The user will type the letter grade that it wants in this part. 

        System.out.println("\nEnter Percentage Weights: \t");

        String input = grade.nextLine();

        // The string above is needed when the user enters the exam grades and averages. 

        System.out.print("\n\nExam 1: \t"); 
        Exam1 = grade.nextShort();
        System.out.print("\nExam 2: \t"); 
        Exam2 = grade.nextShort();
        System.out.print("\nFinal Exam: \t");
        finalExam = grade.nextShort();
        System.out.print("\nLabs: \t");
        Labs = grade.nextShort();
        System.out.print("\nProjects: \t");
        Projects = grade.nextShort();
        System.out.print("\nAttendance: \t");
        Attendance = grade.nextShort();
        System.out.print("\nQuizzes: \t");
        Quizzes = grade.nextShort();

        PercentageWeights = (int)(Exam1 + Exam2 + finalExam + Labs + Projects + Attendance + Quizzes);

        // The equation above will provide the sum of the percentage weights. Since the variables in the equation were 
        // originally declared as doubles, I had to put "int" before the actual equation. 


        if (PercentageWeights > 100 || PercentageWeights < 100) { 
            System.out.println("\nWeights do not add up to 100. Program exiting. Have a nice day!");
            System.exit(0);
        }
        else { 
            System.out.println("\nEnter your scores out of a 100: \t");
        }

        // The part above is very important to continue the rest of the program. If the sum of the percentage weights equals 100, 
        // the program will continue to run. If the sum is greater than or less than 100, the program will terminate. 

        System.out.print("\nDo you know your Exam 1 score?");
        if (input.equalsIgnoreCase("yes")) {
            System.out.print("\nScore received on Exam 1: "); 
            Exam1Grade = grade.nextDouble();
        }
        else{ 
            Exam1Grade = 0;
        }

        System.out.print("\nDo you know your Exam 2 score?");
        if (input.equalsIgnoreCase("yes")) { 
            System.out.print("\nScore received on Exam 2: ");
            Exam2Grade = grade.nextDouble();
        }
        else{ 
            Exam2Grade = 0;
        }
        System.out.print("\nDo you know your final exam score?");
        if (input.equalsIgnoreCase("yes")){ 
            System.out.print("\nScore received on final exam: ");
            finalExamGrade = grade.nextDouble();
        }
        else{
            finalExamGrade = 0;
        }
        System.out.print("\nDo you know your lab average?");
        if (input.equalsIgnoreCase("yes")){ 
            System.out.print("\nAverage lab grade: "); 
            LabAverage = grade.nextDouble();
        }
        else{
            LabAverage = 0;
        }
        System.out.print("\nDo you know your project average?");
        if (input.equalsIgnoreCase("yes")){
            System.out.print("\nAverage project grade: "); 
            ProjectsAverage = grade.nextDouble();
        }
        else{
            ProjectsAverage = 0;
        }
        System.out.print("\nDo you know your quiz average?");
        if (input.equalsIgnoreCase("yes")) {
        System.out.print("\nAverage quiz grade: ");
            QuizzesAverage = grade.nextDouble();
        }
        else{ 
            QuizzesAverage = 0;  
        } 
        System.out.print("\nDo you know your attendance average?");
        if (input.equalsIgnoreCase("yes")){ 
        System.out.print("\nAverage Attendance Grade: ");
            AttendanceAverage = grade.nextDouble();
        }
        else{
            AttendanceAverage = 0;      
        }

        // The user has finished answering the questions. Now the program will automatically calculate the data based on 
        // what the user typed into the program. 

        double CurrentGrade, avgToFinalLetterGrade, WeightandGrade, finalOverallGrade; 

        // The doubles above need to be declared in order for the equations below to work properly. 

        WeightandGrade = (int)((Exam1 * Exam1Grade) + (Exam2 * Exam2Grade) + (finalExam * finalExamGrade) + (Labs * LabAverage) + (Projects * ProjectsAverage) + (Quizzes * QuizzesAverage) + (Attendance * AttendanceAverage));

        CurrentGrade = (int)((WeightandGrade) / (Exam1 + Exam2 + finalExam + Labs + Projects + Quizzes + Attendance ));

        knownWeight = (Exam1 + Exam2 + finalExam + Labs + Projects + Quizzes + Attendance);

        if (grade.equals(A)){ 
            finalOverallGrade = 90;
        }
        else if (grade.equals(B)){ 
            finalOverallGrade = 80;
        }
        else if (grade.equals(C)){ 
            finalOverallGrade = 70;
        }
        else if (grade.equals(D)){ 
            finalOverallGrade = 60;
        }
        else 
            finalOverallGrade = F; 

        avgToFinalLetterGrade = (((100-finalOverallGrade) * (WeightandGrade)) / (100 - knownWeight));

        // The equations above are one of the last parts of the program. These equations are critical to determine whether or not the user received its desired letter grade. 
        // If the desired grade was not reached, the program will give a score that the user must consistently receive in order to possibly reach the desired letter grade.

        if (finalOverallGrade >= 90){ 
            System.out.print("Congratulations! You got an A in the class! Hooray!");
        }
        else if (finalOverallGrade >=80 && finalOverallGrade < 90){ 
            System.out.print("Good job. You got a B in the class!");
        }
        else if (finalOverallGrade >=70 && finalOverallGrade < 80){ 
            System.out.print("You got a C in the class.");
        }
        else if (finalOverallGrade >=60 && finalOverallGrade < 70){
            System.out.print("You got a D in the class.");
        }
        else 
            System.out.print("I'm sorry, but you have a failing grade in the class. May your GPA have mercy on your soul.");
        }

}
Aabglov
  • 498
  • 2
  • 11
  • What is the last line printed to the console before it exits? – Keith Sep 30 '15 at 01:27
  • 1
    `double A = 90-100` then `grade.equals(A)`? Do you really know what you are doing here? And, can you also learn how to give proper namings? I saw some variable using camel case starting with small letter while some start with Capital letter – Adrian Shum Sep 30 '15 at 02:24

2 Answers2

2

There are quite a lot of things wrong with this code. Doing double A=90-100; will set A equal to -10;

However, for your current question:

You do String input = grade.nextLine(); You never change input, and so if input isn't "yes", it will just skip getting the grades for each piece.

(You might want to also consult Using scanner.nextLine() for other pitfalls with using scanner.nextLine() intermixed with scanner.nextInt or similar [in summary: if you do scanner.nextInt, this doesn't consume the newline, so scanner.nextLine() will just get that newline and not the next line after that you might be expecting to get])

Community
  • 1
  • 1
Foon
  • 5,551
  • 11
  • 36
  • 39
0

input = grade.nextLine() reads the remainder of the line with the user's "percentage weights" input on it. So unless the user had a priori knowledge to enter "yes", input will be empty.

I.e., you need to update input with user input before if (input.equalsIgnoreCase("yes")) {....

Keith
  • 2,884
  • 2
  • 16
  • 23