Hey,
So this is my code for my getChoice ( ) method.
private int getChoice ( int lower, int upper ) { System.out.println ( "--------------------------------------------------------------" ); System.out.print ( "Please enter your choice: "); String stringSelection = Keyboard.readInput ( ); System.out.println ( "--------------------------------------------------------------" ); if ( stringSelection.length ( ) == 0 ) { return INVALID; } else { int selection = Integer.parseInt ( stringSelection ); if ( selection >= lower && selection <= upper ) { return selection; } else { return INVALID; } } }
It's meant to take an input, and then if it's not between or equal to lower and upper (in this case the method is being used for lower int = 1, and upper = 4)
then it will return my constant INVALID variable. My Keyboard.readInput() method returns a String as you can see.
At the moment I have it set up so that if the input is empty (now that I think of it I could have just said null instead of .length() ) then it will return invalid.
And if the INTEGER is not between lower and upper, then it will return invalid. But say if I was to type "a22asdf" or something else random, then it will give me an error.
So my question, is how can I make it return INVALID if anything entered as a choice is not between or equal to lower and upper.
Probably a very silly thing that I will understand as soon as someone helps me out. One of those things
(I was thinking of an array and a for loop that cycled through int i)
Thank you very much and regards,
Max