0

I'm trying to input two ints from a user to generate an array. The first number determines how many elements are in the array, and the second will be the maximum value for a random value generator. I will then add all the positions and find the min/max and mean. Right now, I'm trying to go above the call of duty and handle exceptions and do data validation (which isn't required for this assignment).

I'm having trouble with catching the error and trying to re-read the integer from the user. Right now it works perfectly until I enter in a word a second time (after it throws the exception in the first block. If I enter another word it moves on to the next bit of code (the maximum value input).

I'd like to know if there is any other better way of doing this or if I am missing something.

import java.util.InputMismatchException;
import java.util.Scanner;

public class main {

 public static void main(String[] args) {
    boolean dataValid = false;

    Scanner scan = new Scanner(System.in);

        System.out.println("Please enter the number of positions:");

        try     {

            do  {   
                int arrayPositions = scan.nextInt();

                if(arrayPositions < 1)  {
                    System.out.println("You cannot enter a negative number");
                    dataValid = false;
                } else if(arrayPositions > 100) {
                    System.out.println("Please use a number less than 101");
                    dataValid = false;
                } else {
                    dataValid = true;
                }

            } while (dataValid = false);

        } catch (InputMismatchException e)  {
            System.out.println("Please enter a whole integer!");
            System.out.println(scan.next() +" is not a valid input!");
            scan.next();
        }

    dataValid = false;

    do  {

        System.out.println("Please enter the maximum value:");
        int maxValue = scan.nextInt();

        if(maxValue < 1)    {
            System.out.println("Please enter a number greater than 0:");                
            dataValid = false;
        } else if (maxValue > 1000) {
            System.out.println("Please enter a number smaller than or equal to 1000:");
            dataValid = false;
        } else {
            dataValid = true;
        }

    } while(dataValid = false);
    }
}
Abdelhak
  • 8,161
  • 4
  • 19
  • 34
Hart
  • 11
  • `do{...}while (dataValid = false);` `=` is assignment operator, `==` is comparison. – Pshemo Dec 07 '15 at 21:45
  • Also `Scanner` provides `hasNextInt` method so you could prevent throwing `InputMismatchException` exception. – Pshemo Dec 07 '15 at 21:47
  • My bad, I always write that wrong. – Hart Dec 08 '15 at 00:17
  • hasNextInt? I'll look into that. – Hart Dec 08 '15 at 00:17
  • This may help: http://stackoverflow.com/questions/3059333/validating-input-using-java-util-scanner, http://stackoverflow.com/questions/2912817/how-to-use-scanner-to-accept-only-valid-int-as-input – Pshemo Dec 08 '15 at 00:19

0 Answers0