Jump to content

Java probelm help - type validation

Tohrchur

Hey guys so I have this lab due and really need some help. The first part of the lab I had to validate the type of variable input. But the second part is also validating the range of numbers and I cant seem to figure out how to do both within in a loop so that until satisfied itll keep looping.

 

I've tried using the hasNextInt() to get it to make sure that input is an integer. but I cant get it to make sure the range is between 5-21 and loop if it is not because the input could be 4, so its an int and goes past the hasNextInt() check, but then its not valid range and if it asks again I could enter a string and it'd be all messed up.

 

Please help.

 

Heres the prompt. 

mNkuQ.png

Tohrchur

Link to comment
Share on other sites

Link to post
Share on other sites

Hey guys so I have this lab due and really need some help. The first part of the lab I had to validate the type of variable input. But the second part is also validating the range of numbers and I cant seem to figure out how to do both within in a loop so that until satisfied itll keep looping.

 

I've tried using the hasNextInt() to get it to make sure that input is an integer. but I cant get it to make sure the range is between 5-21 and loop if it is not because the input could be 4, so its an int and goes past the hasNextInt() check, but then its not valid range and if it asks again I could enter a string and it'd be all messed up.

 

Please help.

 

Heres the prompt. 

mNkuQ.png

Can you show us your current code please

Link to comment
Share on other sites

Link to post
Share on other sites

You could use an if statement. Something like: if (i <= 25 && i >= 5), i being the number you want to check. 

 

But if you can show us your current code, that'd could help us help you.

Link to comment
Share on other sites

Link to post
Share on other sites

can't you just store the input in a variable, then check if it is within the correct range? if it is not then just go back and ask for input again

Link to comment
Share on other sites

Link to post
Share on other sites

You could use an if statement. Something like: if (i <= 25 && i >= 5), i being the number you want to check. 

 

But if you can show us your current code, that'd could help us help you.

the problem is that i could enter a 3 right? and it would pass the integer test. then it would go to the range test and would fail. so it'd ask again but between 5-21. but at that point i could enter "adkfrjadsf" and it would go through since it passed the integer test already. Here is what i have so far. Ive written like 5 different programs trying to figure it out so i tried to get this real quick cuz the rest were ugly.

mNnak.png

Tohrchur

Link to comment
Share on other sites

Link to post
Share on other sites

snips

The problem is that you need to go back to the beginning of the entire procedure, while your code currently stalls and continues from the middle of the procedure, thus missing out on crucial steps. 

 

One option to go back to the beginning is to call the method again and return.

public static void procedure() {    System.out.println("Beginning of procedure");    if (new Random().nextBoolean()) {        System.out.println("Oh no! The condition failed and we have to restart!");        procedure();        return;    }    System.out.println("The condition succeeded, and we can now carry on.");}

If the entire procedure starts with the #procedure method, then calling that method will make your program go through it again.

Once that method finishes executing, however, your program will continue running from the point you called #procedure. That's why we have to return after calling #procedure - so that your program doesn't try to finish the second half of the method again. 

Link to comment
Share on other sites

Link to post
Share on other sites

I rewrote the program and it works for the most part. But if i enter an "invalid number" (not between 5-21) it wont exit that loop and will always give  the "invalid number" response and never continue to the rest of the program (which has yet to be written)

 

mNtnc.png

Tohrchur

Link to comment
Share on other sites

Link to post
Share on other sites

I would do like this:

Scanner sc = new Scanner(System.in);int num;boolean isIntAndInRange = false;while(!isIntAndInRange) {System.out.println("Enter integer:");if (!sc.hasNextInt()) {System.out.println("That's not an integer, try again!");isIntAndInRange = false;} else {isIntAndInRange = true;}num = sc.nextInt();if (num >= 5 && num <= 21 && isIntAndInRange == true) {isIntAndInRange = true;} else {isIntAndInRange = false;System.out.println("not in valid range");}}//Here put the code for what to do after you validate the input

PS.: I did not test the code so probably there will be needed some changes

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×