fred rosenberger wrote:I would suggest you stop writing code. slow down. back up a bit.
Programming is really 90% thinking, and 10% typing. (Some may disagree on the exact percentages <cough><cough><Campbell><cough><cough>).
I try to never write code until i've thought through the problem A LOT. What do i really want to do? When do I want to do it? What is a good way to accomplish my task? What is the MINIMUM amount of code I can write to add value to what I want to do?
Assuming your ArrayList is populated, start by JUST printing out each value in the list. Once that's done, try only printing values greater than the minimum. Once that works, add in the condition to not print the maximum.
If you look at your current code, you have a loop that iterates the same number of times as there are elements in the list, but you don't actually look at the elements in the list. Then, if there were 30 elements in the list, you'd print out your error messages (potentially) that many times.
Consider writing lots of small methods. write a method to get input. Write a method to validate input. Avoid hard-coding values in the middle of your code. Define a variable like "final int upperLimit = 9", then use that variable in your code - that makes it easier to update later, when your specs change.
Paul Clapham wrote:
Suane Mane wrote:Also I thought the "else..." part of the code covered the part that would have to be in the try block because that covers all the numbers that can be entered from 0 to 9, which means it can hold 2.5, 4.6, 7.3 etc.
No. A try-block is specifically for catching exceptions. It's the Integer.parseInt() method that will throw an exception, so it's the code that needs to be in the try-block. The rest of the code checking for < 0 and > 9 doesn't need to be in the try-block and the code might be clearer if it wasn't.
Carey Brown wrote:
Piet Souris wrote:
a way is to put this part in main(), and give these variables as arguments to your constructor. What I would certainly do is to put some text before usng scanner,nextInt, for instance:
Then the user sees that he/she is supposed to input something.