-1

I am wishing to prompt the user again if a double outside of the accepted range (0-100) is input, until the input is valid. When the input is considered valid, I am wanting to return correct input value, yet, returned instead is the first incorrect value. How can I return the correct input, as accepted by the if statement?? Many thanks!

public class examscore {
    public static void main (String[] args) {
        Scanner console = new Scanner(System.in);
        double sumfin = finalscore(console); 
        System.out.println(sumfin); // if user input is initially invalid, but then corrected, the first, incorrect, value is printed
    }

    public static double finalscore (Scanner console) { 
        System.out.println();
        System.out.println("Input final exam score: ");

        while(!console.hasNextDouble()) { //prompts the user, if invalid input, to input again, until a valid value is input
            System.out.println("Please input a mark between 0 and 100. ");
            console.next();
        } 
        double examscore = console.nextDouble();

        if (examscore >=0 && examscore<= 100) {
            System.out.println();
            System.out.println("Exam Score = "+examscore);
        } else {
            System.out.println("Error:");
            finalscore (console);
        }
        return examscore; //an attempt to return the VALID exam score: fails
    }
}
Andreas
  • 138,167
  • 8
  • 112
  • 195

3 Answers3

1

A do-while loop would be a perfect fit. Example:

Scanner console = new Scanner(System.in);
double userInput = 0;
do {
    System.out.println("Please input a mark between 0 and 100. ");
    try {
        userInput = console.nextDouble();
    } catch (InputMismatchException e) {
        System.out.println("Your input could not be interpreted as a floating-point number.");
    }
} while (userInput <= 0D || userInput >= 100D);
Grzegorz Górkiewicz
  • 3,993
  • 4
  • 19
  • 35
  • If the user enters something that doesn't fit into a double, a exception is thrown and the program stops – davidxxx Jan 28 '17 at 20:28
0

You missed to assign result of finalscore(console) to examscore inside the else block.

        if (examscore >= 0 && examscore <= 100) {
            System.out.println();
            System.out.println("Exam Score = " + examscore);
        } else {
            System.out.println("Error:");
            examscore = finalscore(console);
        }
Monzurul Haque Shimul
  • 7,005
  • 2
  • 19
  • 34
0

You can either use a loop or a recursive call to accomplish this. I prefer a recursive call:

private static double getValidScore(Scanner console) {
    System.out.println();
    System.out.println("Input final exam score: ");
    try {
        double score = Double.parseDouble(console.nextLine());
        if (score >= 0 && score <= 100) {
            return score;
        }
    } catch (NumberFormatException nfe) {}
    System.out.println("Please input a mark between 0 and 100.");
    return getValidScore(console);
}
4castle
  • 28,713
  • 8
  • 60
  • 94
  • 1
    Why `Double.parseDouble(console.nextLine())` instead of `console.nextDouble()`? – Grzegorz Górkiewicz Jan 28 '17 at 20:27
  • 1
    @GrzegorzGórkiewicz It provides better error handling, in case a non-double is entered. – 4castle Jan 28 '17 at 20:28
  • 1
    Any arguments to back it up? – Grzegorz Górkiewicz Jan 28 '17 at 20:31
  • 1
    @GrzegorzGórkiewicz There is some discussion of it [here](http://stackoverflow.com/a/26586629/5743988). – 4castle Jan 28 '17 at 20:35
  • `double score = Double.parseDouble(console.nextLine());` is less readable and straight than `double score = console.nextDouble();` And in both cases the exception should be handled. So I would favor the second one way to get only a double value. – davidxxx Jan 28 '17 at 21:09
  • Thank-you very much; I have not yet used try catch blocks, but after some further reading, have realized their importance and utility. Using try catch has resulted in my code being much less lengthy and complex! – Trixtan Zzaborniak Jan 28 '17 at 21:49