• Post Reply Bookmark Topic Watch Topic
  • New Topic

Validating input  RSS feed

 
bernie benavides
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to have a user enter a date but i need to make sure that the inputs are only digits if not have the user re enter input. what would be a concise way to do it. I tried using a while loop that accepted inputs for month, day, year. but since I am not very experienced my program is huge can someone explain to me how to reduce the repetitveness of my program and also how to get out of this while loop. every thing I try is not working.



Edit by mw: Put code inside code tags.
[ February 12, 2008: Message edited by: marc weber ]
 
Daesung Park
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can give you a hint.
1. Refactoring(Extract Method) will help remove redundancy.
2. Check if all conditions are satisfied. If yes, just "break" loop.
 
bernie benavides
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry I havent seen that method before. How would you write the code to break out if all conditions are met in the three if statements.
 
Mark Newton
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd disagree with Daesung Park here - using the 'break' keyword is not great programming practice.

Sure - it works, but it can make it harder to maintain your code, and for others to understand. I'd say as a beginner, it is a very bad habit to get into.

I've only had a cursory glance over your code, but I would just set ok = false when you have confirmed that you don't want to loop again.

Also, you should try to UseCodeTags, when you post code.
 
bernie benavides
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did try that but that but it wouldnt break out of the loop I didnt really know where to put I tried under the for loops but it would only verify the first for loop. I would appreciate it if someone can explain to me how to break out of the loop after the three conditions are met ??
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pseudo code (basic idea)...
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!