Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop to validate user input

Tags:

java

loops

I am fairly new to Java and I am trying to write a small program that asks a user to enter 3 integers between 1-10, stores them in an array and then adds up the integers and tells the user the answer. I have written this so far and it works:

import java.util.Scanner;

public class Feb11a {

    public static void main(String[] args) {

        int[] numArr = new int[3];
        int sum = 0;
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter 3 numbers in the range 1 to 10: ");

        for (int i = 0; i < numArr.length; i++) {
            numArr[i] = keyboard.nextInt();
        }

        for (int counter = 0; counter < numArr.length; counter++) {
            sum += numArr[counter];
        }

        System.out.println("The sum of these numbers is " + sum);
    }
}

My problem is I am also meant to validate the input as in if they enter a double, a string or a number outside the 1-10 range. I have tried a while loop but I just cannot get the program to work, below is what I have so far. If I take out the first while loop the second one works i.e. it checks if it is an integer:

import java.util.Scanner;

public class Feb11a {

    public static void main(String[] args) {

        int[] numArr = new int[3];
        int sum = 0;
        Scanner keyboard = new Scanner(System.in);

        for (int i = 0; i < numArr.length; i++) {
            //check if between 1 and 10
            while (i > 10 || i < 1) {
                System.out.println("Enter a number in the range 1 to 10: ");

                //check if integer      
                while (!keyboard.hasNextInt()) {
                    System.out.println("Invalid entry, please try again ");
                    keyboard.next();
                }
                numArr[i] = keyboard.nextInt();
            }
        }

        for (int counter = 0; counter < numArr.length; counter++) {
            sum += numArr[counter];
        }

        System.out.println("The sum of these numbers is " + sum);
    }
}

My question is how do I get it to check if it is an integer and if it is the range 1-10?

like image 918
Mary Kelly Avatar asked Oct 01 '22 15:10

Mary Kelly


1 Answers

import java.util.Scanner;

public class NewClass {

public static void main(String[] args) 
{

int[] numArr = new int[3];
int sum=0,x;
Scanner keyboard = new Scanner(System.in);



  for(int i=0; i<numArr.length; i++)
 {
        //check if between 1 and 10
     System.out.println("Enter a number in the range 1 to 10: ");

                //check if integer      
    while (!keyboard.hasNextInt())
    {
        System.out.println("Invalid entry, please try again ");
        keyboard.next();
    }
            x = keyboard.nextInt();
           if(x>0 && x<=10)
             numArr[i]=x;
           else{
               System.out.println("Retry Enter a number in the range 1 to 10:");
               i--;
           }

}


     for (int counter=0; counter<numArr.length; counter++)
    {
        sum+=numArr[counter];
    }
    System.out.println("The sum of these numbers is "+sum);
 }

}
like image 171
Danyal Avatar answered Oct 12 '22 00:10

Danyal