Identifying Invalid input
I need a bit of code such as else if () that will allow my scanner to be identified as invalid, invalid inputs would be anything that is not and integer ranging from 2 to 10.
Re: Identifying Invalid input
Steps;
1. First create a scanner object e.g Scanner sc = new Scanner(System.in)
2. Read the value from keyboard and store it into int varialbe e.g int value = sc.nextInt();
3. Now check if value is within the range or not?
if(value < 2 || value > 10)
{
// then this in not in range(Invalid)
}
else
{
//within the range(valid)
}
Re: Identifying Invalid input
Quote:
Originally Posted by
Amok
Steps;
1. First create a scanner object e.g Scanner sc = new Scanner(System.in)
2. Read the value from keyboard and store it into int varialbe e.g int value = sc.nextInt();
3. Now check if value is within the range or not?
if(value < 2 || value > 10)
{
// then this in not in range(Invalid)
}
else
{
//within the range(valid)
}
Fine; now suppose I type "foobar"; is that in range? Guess what would happen. Making your code monkey (me) proof takes a bit more ...
kind regards,
Jos
Re: Identifying Invalid input
@JosAH, we can always put int value = sc.nextInt(); within try/catch block. If its a String then we can catch it within catch block and print a message saying its not in a range. I m sorry if i am wrong. There are many way we can do it and its a programmer choice how to approach to it, thats why i didn't mentioned it.
Re: Identifying Invalid input
Quote:
Originally Posted by
Amok
@JosAH, we can always put int value = sc.nextInt(); within try/catch block. If its a String then we can catch it within catch block and print a message saying its not in a range. I m sorry if i am wrong. There are many way we can do it and its a programmer choice how to approach to it, thats why i didn't mentioned it.
Yep, but keep in mind that if the user didn't type any digits, the nextInt() method won't read anything, so a next call to the nextInt() method chews on those same none-digits again; you have to get rid of them (in the catch-clause). Making input handling really monkey proof takes a bit more than try-catching the conflicting input ...
kind regards,
Jos
Re: Identifying Invalid input
Re: Identifying Invalid input
Re: Identifying Invalid input
Quote:
Originally Posted by
JosAH
Yep, but keep in mind that if the user didn't type any digits, the nextInt() method won't read anything, so a next call to the nextInt() method chews on those same none-digits again; you have to get rid of them (in the catch-clause). Making input handling really monkey proof takes a bit more than try-catching the conflicting input ...
kind regards,
Jos
Just remember that once a programmer makes his software "idiot proof", someone comes along with a better idiot. :P:
Re: Identifying Invalid input
Quote:
Originally Posted by
Keith Jackson
Just remember that once a programmer makes his software "idiot proof", someone comes along with a better idiot. :P:
Yes, isn't life cruel?
kind regards,
Jos