Results 1 to 9 of 9
Thread: Identifying Invalid input
- 09-30-2012, 04:03 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 54
- Rep Power
- 0
- 09-30-2012, 04:17 PM #2
Member
- Join Date
- Sep 2012
- Location
- Australia
- Posts
- 12
- Rep Power
- 0
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)
}
- 09-30-2012, 04:22 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 09-30-2012, 04:30 PM #4
Member
- Join Date
- Sep 2012
- Location
- Australia
- Posts
- 12
- Rep Power
- 0
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.
- 09-30-2012, 04:51 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: Identifying Invalid input
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 09-30-2012, 04:58 PM #6
Member
- Join Date
- Sep 2012
- Location
- Australia
- Posts
- 12
- Rep Power
- 0
Re: Identifying Invalid input
Thanks alot
- 09-30-2012, 05:24 PM #7
Member
- Join Date
- Feb 2012
- Posts
- 54
- Rep Power
- 0
Re: Identifying Invalid input
So what should I do? lol
- 10-02-2012, 07:55 PM #8
Member
- Join Date
- Sep 2012
- Location
- Maryland, USA
- Posts
- 14
- Rep Power
- 0
- 10-02-2012, 08:33 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
identifying a socket exception
By yemista in forum NetworkingReplies: 2Last Post: 12-01-2011, 07:07 PM -
Identifying Objects/Variables
By hiralarious in forum New To JavaReplies: 2Last Post: 09-20-2011, 03:44 AM -
Help me ! identifying the mistake
By o0oNorao0o in forum New To JavaReplies: 6Last Post: 11-06-2010, 10:03 PM -
Most of my trouble is identifying a pattern
By ElkNinja in forum New To JavaReplies: 4Last Post: 10-10-2010, 06:17 AM -
help with identifying components of a gui
By jaytee in forum New To JavaReplies: 1Last Post: 03-08-2010, 02:28 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks