This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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):
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..
Joined: Sep 14, 2012
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...