0

I have a problem with the following code. It's supposed to ask the user a series of questions about students' test scores and return the average test score, this part is working okay.

My problem is that I'm supposed to use a "while-loop" which will run the program, then ask the user if they want the program to end. If they respond "no," then it should restart and ask for a new set of test scores, etc.

When I try to execute the program it works fine up until the part where it's supposed to restart. After printing out the average it will ask me if I want to close the program, but it won't allow me to type anything in. It's like the program is ending on it's own.

I've made some comments around the while loop explaining my logic as I suspect that's where the problem will be.

import java.util.Scanner;
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class testScores {
    public static void main(String[] args)  {
        NumberFormat formatter = new DecimalFormat("####.##");
        Scanner keys = new Scanner(System.in);
// Initialize endProgram to "no"
        String endProgram = "no";
// Declare there's going to be a string called end
        String end;
        double totalScore = 0;
        double averageScore = 0;
        do  {
// Do all of this now before you check anything
            int number = getNumber(keys);
            totalScore = getScores(number, keys, totalScore);
            averageScore = getAverage(totalScore, number, averageScore);
            printAverage(averageScore, formatter);
            System.out.println("Do you want to end the program?");
            end = keys.nextLine();
// Now check if end and endProgram contain the same string
        }  while (end.equals(endProgram));
// Apparently not
    }
        public static int getNumber(Scanner keys)  {
        System.out.println("How many students took the test?");
        int number = keys.nextInt();
        return number;
    }
    public static double getScores(int number, Scanner keys, double totalScore)  {
        for(int counter = 1; counter <= number; counter++)  {
            System.out.println("Enter their score.");
            double score = keys.nextDouble();
            totalScore = totalScore + score;
        }
        return totalScore;
    }
    public static double getAverage(double totalScore, int number, double averageScore)  {
        averageScore = totalScore / number;
        return averageScore;
    }
    public static void printAverage(double averageScore, NumberFormat formatter)  {
        System.out.println("The average is " + formatter.format(averageScore));
    }
}

I've tried a few different things like using a while instead of a do-while, and switching out "end" for "endProgram" to have it check "endProgram" after resetting it's value, all with no success. If anyone could help me with this it would be greatly appreciated.

soop
  • 13
  • 2
  • You probably want to look at this [question and answer](http://stackoverflow.com/questions/26446599/how-to-use-scanner-to-correctly-read-user-input-from-system-in-and-act-on-it) as well for a full and complete solution to using `java.util.Scanner` correctly. –  Oct 07 '15 at 20:57

1 Answers1

-1

You should try something along this line. The gist is that - the do-while is wrong here because it checks against a non-existant String :

    String end = "";
  /* rest of variables */

       while (!(end.equals(endProgram) )) {
// Do all of this now before you check anything
            int number = getNumber(keys);
            totalScore = getScores(number, keys, totalScore);
            averageScore = getAverage(totalScore, number, averageScore);
            printAverage(averageScore, formatter);
            System.out.println("Do you want to end the program?");
            end = keys.nextLine();
// Now check if end and endProgram contain the same string
        }  
Caffeinated
  • 10,270
  • 37
  • 107
  • 197