Using do-while loop to send an error message when input is out of bounds

Viewed 27

Here I am using a do while loop to execute a program that allows the user to score student scores in an array and then print them. I am struggling with implementing a system that sends an error message when the user inputs a number below 0 or above 100 and then lets the user try again.

import java.util.Scanner;

public class storeScore {

    public static void main(String [] args) {
   
        Scanner input = new Scanner (System.in);

        int scores [] = new int [7];
        int numberOfStudents = 7;


        //User input all scores. For loop works by asking for user input until it reaches the number as input in numberOfStudents.
        do 
        {
        for(int i = 0; i<numberOfStudents; i++) {

            scores[i] = input.nextInt();
           
            if (i == 0) {
                System.out.print("Enter the score for the 1st student: ");
            }
            else if (i == 1) {
                System.out.print("Enter the score for the 2nd student: ");
            }
            else if (i == 2) {
                System.out.print("Enter the score for the 3rd student: ");
            }
            else if (i >= 3) {
                System.out.print("Enter the score for the " + (i+1) + "th student: ");
            }
           
        } 
        
        //Error output if input is incorrect
        }          
        while (scores[i] < 0 || scores[i] > 100) {

            System.out.println("Input out of bounds. Score can only be between 0 and 100");

        }

      //Printing all scores.
        System.out.println("Thank you for your input. Your entered scores are: ");

        for (int i=0; i<numberOfStudents; i++)  
                {  
                    System.out.print(scores[i] + ", ");  
                }  
       
    
 input.close();
       
   
    }
   

}

1 Answers

You have your error message printing in a while loop without any way of breaking out of that loop.

Try capturing your user's input as an int variable first that you can check to see if it's valid before assigning it to scores[].

for(int i = 0; i<numberOfStudents; i++) {
        String message;
       
        if (i == 0) {
            message = "Enter the score for the 1st student: ";
        }
        else if (i == 1) {
            message = "Enter the score for the 2nd student: ";
        }
        else if (i == 2) {
            message = "Enter the score for the 3rd student: ";
        }
        else if (i >= 3) {
            message = "Enter the score for the " + (i + 1) + " student: ";
        }

        System.out.println(message);

        int score = input.nextInt();
        
        //Now check if the input value is valid
        while (score < 0 || score > 100) {
            System.out.println("Input out of bounds. Score can only be between 0 and 100");

            System.out.println(message);
   
            score = input.nextInt();
        }

        score[i] = score;
    } 

Now, the loop will print the message for student i, then take in the score. If the score is invalid, the while loop will print out the error message, and take input again. If the input is valid this time, the while loop breaks and assigns the new score to student i, otherwise it reprints the error message and takes input again.

Related