Skip to content
Advertisement

how to make the program to keep asking until the a valid number is entered?

i have made a code that can ask user to input the number of student and ask to input the grade of that student, but i can’t make the program do just like the example. i tried using “do…While” and it did ask to enter the grade again for student 1 only, when i enter an invalid number for student 2, it reset back to sudent1. My question is how to make the program to keep asking until the a valid number is entered? Just like the sample below where the program ask the grade again for student2.

  • A sample session is as follow:
  • Enter the number of students: 3
  • Enter the grade for student 1: 55
  • Enter the grade for student 2: 108
  • Invalid grade, try again…
  • Enter the grade for student 2: 56
  • Enter the grade for student 3: 57
  • The average is 56.0 */
import java.util.Scanner;
public class Assingment 
{

    public static void main(String[] args) 
    {
        Scanner kb = new Scanner(System.in);
        int index, size;
        double totalMarks = 0;
        
        System.out.println("Enter the number of students : ");  // ask user to input the number of students
        size = kb.nextInt();               // for determine array size
        
        if (size <= 0) 
        {
            System.out.println("Invalid number of students.");
            return;
        }
        
        int [] marks = new int[size];   // declare array for marks
        
        do
        {
            for(index = 0; index<size; index++)
            {
                System.out.print("Enter the grade of student " + (index+1) + " : ");
                marks[index] = kb.nextInt();

                // chek if grade is smaller than 0 or larger than 100
                if(marks[index] < 0 || marks[index] > 100) 
                {
                    System.out.println("Invalid grade, try again...");
                    break;
                }
                else
                {
                    totalMarks = totalMarks + marks[index];
                }
            }
        }while(marks[index] < 0 || marks[index] > 100);
        
        System.out.println("The average is " +totalMarks);
                    
    }
}

Advertisement

Answer

The issue is your for loop progresses forward no matter what you do and it increases the index. I think you just need a while loop that keeps running until the correct value is inserted.

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner kb = new Scanner(System.in);
        int index, size;
        double totalMarks = 0;

        System.out.println("Enter the number of students : ");  // ask user to input the number of students
        size = kb.nextInt();               // for determine array size

        if (size <= 0) {
            System.out.println("Invalid number of students.");
            return;
        }

        int[] marks = new int[size];   // declare array for marks

        for (index = 0; index < size; index++) {
            System.out.print("Enter the grade of student " + (index + 1) + " : ");
            marks[index] = kb.nextInt();

            while (true) {
                if (marks[index] < 0 || marks[index] > 100) {
                    System.out.println("Invalid grade, please enter again : ");
                    marks[index] = kb.nextInt();
                } else {
                    totalMarks = totalMarks + marks[index];
                    break;
                }
            }
        }

        System.out.println("The average is " + totalMarks);

    }
}
Advertisement