This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Problems with input validation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problems with input validation" Watch "Problems with input validation" New topic
Author

Problems with input validation

Michael Payte
Greenhorn

Joined: Sep 14, 2012
Posts: 14
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

Joined: Aug 11, 2012
Posts: 370
    
    1

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

Joined: Mar 26, 2012
Posts: 186

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,


The biggest gamble will be to ask a question whose answer you know in that it will challenge your theory | www.TechAspire.blogspot.in
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 370
    
    1

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

Joined: Sep 14, 2012
Posts: 14
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

Joined: Mar 26, 2012
Posts: 186

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:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problems with input validation
 
Similar Threads
Can't add to ArrayList within switch statement?
Maybe I miss something
Adding products of numbers in an array, and string output problem
Creating array of objects from user input and saving to a file
try...catch blocks giving <identifier> expected error