• Post Reply Bookmark Topic Watch Topic
  • New Topic

Input validation: Re-prompt user in main menu if they enter wrong input format  RSS feed

 
Sina Meraji
Greenhorn
Posts: 13
Java Mac OS X Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm building a small auction system. this is my main menu (UI).
In each switch case, I prompt user to enter some values (item name, date, price etc.). I wanna re-prompt user to re-enter values for each prompt if the initial entry is in wrong format or simply wrong.

for example, if program asks user to enter date and user enters invalid input, i want the user to be re-prompted. I tried a couple of different things but the result I got was repeating all question from beginning (name, startDate, minBidAmount etc) if [for example] startDate was wrong.

one way that i can think of (which sounds bad) is creating one while loop for each question!!

Any tips?



where

and will prompt user to enter Date and time and verify correct format.
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If each prompt and validation was its own method then that method can simply loop until it's got valid data:

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sina Meraji wrote:...will prompt user to enter Date and time and verify correct format.

Dates and times are particularly tricky, since you have 6 components (I assume), ALL of which might have something wrong with them.

One approach is to take each component separately, ie, year, then month, then day, then hour...etc, etc. Pretty simple, but maybe too laborious for your "users".

Another would be to use a SimpleDateFormat to parse the whole string, and use the messages from its Exceptions. Trouble is: I don't know how good they are.

You might also get some tips from this page.

HIH

Winston
 
Sina Meraji
Greenhorn
Posts: 13
Java Mac OS X Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Sina Meraji wrote:...will prompt user to enter Date and time and verify correct format.

Dates and times are particularly tricky, since you have 6 components (I assume), ALL of which might have something wrong with them.

One approach is to take each component separately, ie, year, then month, then day, then hour...etc, etc. Pretty simple, but maybe too laborious for your "users".

Another would be to use a SimpleDateFormat to parse the whole string, and use the messages from its Exceptions. Trouble is: I don't know how good they are.

You might also get some tips from this page.

HIH

Winston


I use this to validate date and type. all good except it doesn't detect an input like hour 26:00. (i'll do another topic for that )


thanks
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sina Meraji wrote:I use this to validate date and type. all good except it doesn't detect an input like hour 26:00.

Well, you haven't shown us your isValidDate(String) method, so it's difficult to comment.
However, I think the idea of such a method is very good (it's called 'single responsibility').

The only thing I would say is that your validDateUI() method should return a Date, not a String.
Have a look here to find out why.

And do have a look at SimpleDateFormat:
1. It'll save you a LOT of code.
2. It can be used for validating AND formatting - in the exact style you're using.

HIH

Winston
 
Sina Meraji
Greenhorn
Posts: 13
Java Mac OS X Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Sina Meraji wrote:I use this to validate date and type. all good except it doesn't detect an input like hour 26:00.

Well, you haven't shown us your isValidDate(String) method, so it's difficult to comment.
However, I think the idea of such a method is very good (it's called 'single responsibility').

The only thing I would say is that your validDateUI() method should return a Date, not a String.
Have a look here to find out why.

And do have a look at SimpleDateFormat:
1. It'll save you a LOT of code.
2. It can be used for validating AND formatting - in the exact style you're using.

HIH

Winston


Thanks Winston

I read the article. Great insights
here's my isValidDate btw

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sina Meraji wrote:here's my isValidDate btw

Very nice.

One small, but rather important, point. When you use SimpleDateFormat for validation, it should be "strict", so add:
  myDateFormat.setLenient(false);
at line 3.

You might also want to think about putting that section of code into a DateValidator class that wraps (or possibly extends) a strict SimpleDateFormat.

Winston
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!