• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Problems with input validation

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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):
 
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Ranch Hand
Posts: 233
1
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 233
1
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Tomorrow is the first day of the new metric calendar. Comfort me tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic