Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problems with input validation

 
Michael Payte
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, so for this program I'm trying to make, user input is required for several integer values. I've made it so that if the user enters a value other than an integer, the program will output that the user input must be an integer, and prompt for input again. The problem is, though, the user input here is then applied to the next thing that requires input, completely skipping what the user had given improper input in the first place. For example, if the user inputted a String when prompted for hours worked on Monday, and they enter "8" when prompted again after the error message appears, that 8 will not be applied to hours worked on Monday, but rather the next thing the program asks for input for: hours worked on Tuesday. The program just assigns a zero to hours worked on Monday. I would really appreciate help in fixing this problem. Thanks!

My program(the problem area is 125-199. Anything that appears as >< is just supposed to be <. I'm not sure why it shows up like that when I paste the code into the forums):
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Payte wrote:

Take a look at your while.. What will happen if your condition becomes false (i.e., input entered is an integer)
You are doing nothing after that, so how will it get set to your required attribute??
 
Rajdeep Biswas
Ranch Hand
Posts: 224
1
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The following idea might help.

Edit: Ahh!! yes, try to make a more generic method and build the validation logic inside that, and enjoy OOP modularity...
Regards,
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are reading so much of values from user, then I would not prefer reading it this way..
You can see so much try-catch block back to back in your code..

Indeed, your main method should never be so long.. All your job is done in main method..
A better way would be to create a method that reads user input and also handles exception according to your need..

Now, for every value to be read, just call that method, it will get your job done..

And your getUserInput() will contain the code of reading integer values from user..

Try to create different methods for different functionality, and invoke those methods from your main in order.
Like, there can be: -
1). separate method for printing all your attributes,
2). a separate method for doing some calculation on user-input, or attributes,
3). a separate method for taking user input..

Avoid keeping your main method doing all the works..
 
Michael Payte
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajdeep Biswas wrote:Hi,
The following idea might help.

Edit: Ahh!! yes, try to make a more generic method and build the validation logic inside that, and enjoy OOP modularity...
Regards,

What's br? It's not defined anywhere.
 
Rajdeep Biswas
Ranch Hand
Posts: 224
1
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NOTE: This is just a sample, you have to customize it.

Michael Payte wrote:What's br? It's not defined anywhere.


And you should call this method from your application, like following:
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic