OrochimaruKid
OrochimaruKid

Reputation: 3

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.

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);
                    
    }
}

Upvotes: 0

Views: 1181

Answers (3)

Aayush adhana
Aayush adhana

Reputation: 1

    System.out.println("Enter your marks by pressing 1 ");
    Scanner sc = new Scanner(System.in);

    int a = sc.nextInt();
    switch (a) {
      case 1:
        System.out.println("Enter your marks");
        break;
      case 2: 
        System.out.println("Program done");
        break;
      default: System.out.println("Wrong button pressed");
    }

    int marks = sc.nextInt();
    if (marks >= 90) {
       System.out.println("These are good marks");
    } else if (marks >= 80) {
      System.out.println("Very nice");
    } else if(marks >= 0 ) {
      System.out.println("Better luck next time");
    }

Upvotes: 0

Pubudu Sitinamaluwa
Pubudu Sitinamaluwa

Reputation: 978

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);

    }
}

Upvotes: 0

Stultuske
Stultuske

Reputation: 9437

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); // validation check

You have added the check at 'validation check', but at that point, you have already added a number, broken out of your loop because of invalid (or continued). You need to put that check at the place where you add the value:

        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
            while ( marks[index] < 0 || marks[index] > 100 ) {
               System.out.println("You've added an invalid grade, try again.");
               marks[index] = kb.nextInt();
            }
            totalMarks = totalMarks + marks[index];
       }

This way, you can only have a correct value, and there's no more need for an if-else structure.

Upvotes: 1

Related Questions