1

I have a HOMEWORK assignment that involves users inputs.

I want to ask the user for three integer inputs in the range 1-7 and store them in an array.

What I have so far seems to validate properly if all inputs are over 7 and rules out strings etc inputs and but still allows for a single input to be over 7.

Any help is appreciated.

    Scanner in = new Scanner(System.in);
    boolean valid = false;
    int[] inputRange = new int[3];
    while(!valid)
    {
        System.out.println("enter three numbers: ");
        if(in.hasNextInt())
        {
            for(int i = 0; i< inputRange.length; i++)
            {
                inputRange[i] = in.nextInt();
                if(inputRange[i] >= 1 && inputRange[i] <= 9){
                    valid = true;
                }
            }
        }else{
            in.next();
        }
    }
  • You should have `valid` start out `true`, then then set it to `false` when it finds something wrong. – khelwood Jan 10 '17 at 16:39
  • Your expression `inputRange[i] <= 9` doesn't comply with your description of the assignment if {1 <= x <= 7} is what you need. And what do you mean with "validate properly if all inputs are over 7"? Isn't this wrong as well? – eli Jan 10 '17 at 16:54

2 Answers2

1

Your logic is fine, but you need to restart valid to false again each time user is going to enter a new digit.

Here's how you can validate user input to be between 1-9 with a do-while using your same logic just a little bit different.

Also next time be sure to post a valid MCVE and not just "snippets" (it should include a main method and imports)

import java.util.Scanner;

public class ValidationOfNumbers {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        boolean valid = false;
        int[] inputRange = new int[3];
        int counter = 0;
        int number = 0;
        System.out.println("Enter 3 digits between 1-9"); //Ask for digits, numbers can have multiple digits, while digits are numbers from 0-9
        for (int i = 0; i < inputRange.length; i++) {
            valid = false; //Restart "valid" variable for each new user input
            do {
                number = in.nextInt();
                if (number >= 1 && number <= 9) {
                    valid = true; //If valid, it will exit do-while
                } else {
                    System.out.println("Enter a valid digit between 1-9");
                }
            } while (!valid);
            inputRange[i] = number; //We know it's valid because it went out of do-while, so we can now store it in the array
        }

        for (int i = 0; i < inputRange.length; i++) {
            System.out.println(inputRange[i]);
        }
    }
}
Community
  • 1
  • 1
Frakcool
  • 10,088
  • 9
  • 41
  • 71
  • Thank you, that is great. The only thing it doesnt do is to check if anything other than a number was entered. Is there an adjustment that can be made to fix that? I tried fiddling around with the code but cant figure it out – A. Einstein Jan 10 '17 at 17:11
  • @A.Einstein please check [this answer](http://stackoverflow.com/a/23839837/2180785) it says how. Don't forget to upvote and accept the [answer](http://stackoverflow.com/help/accepted-answer) if it solved your question. Also check the accepted answer and others there in the same question. – Frakcool Jan 10 '17 at 17:17
-1

Here is the code

    Scanner in = new Scanner(System.in);
    int count = 0;
    int data[] = new int[3];
    while(count < 3) {
        if(in.hasNextInt()) {
            int val = in.nextInt();
            if(val>=1 && val <=7) {
                data[count] = val;
                count++;
            }
        }
        else {
            in.next();
        }
    }
Aditya K
  • 416
  • 3
  • 8
  • 1
    Not the downvoter but it could be probably because you didn't explain why this solves OP's question and why he was wrong – Frakcool Jan 10 '17 at 16:59
  • No worries :) You can read [answer] for more information about how to make your answers even better – Frakcool Jan 10 '17 at 17:02