Re: User selection problem
Quote:
Originally Posted by
zipstacrack
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
Hello Max!
If I'm getting it right you want the method to return INVALID when it throws an exception. I think you can do that by surrounding the part of code that may through the exception (in your code the parsing statement and inner if...else's block) with a try...catch block and when you "catch" the exception you can return INVALID.
Hope it helps.
Re: User selection problem
Quote:
Originally Posted by
andreas90
Hello Max!
If I'm getting it right you want the method to return INVALID when it throws an exception. I think you can do that by surrounding the part of code that may through the exception (in your code the parsing statement and inner if...else's block) with a try...catch block and when you "catch" the exception you can return INVALID.
Hope it helps.
Thank you very much for your reply,
I am not familiar with try...catch.
Could you possibly give me a small example of how it would work with this/or something similar to this?
Regards,
Max
Re: User selection problem
Quote:
Originally Posted by
zipstacrack
Thank you very much for your reply,
I am not familiar with try...catch.
Could you possibly give me a small example of how it would work with this/or something similar to this?
Regards,
Max
Code java:
try {
//code that may throw the exception, the parsing method
} catch (ExceptionThatMayBeThrown e) {
e.getStackTrace();
return INVALID;
}
To understand which return statement is executed (since INVALID will be returned in multiple cases) I would also suggest you print a message before you return anything .
Re: User selection problem
Quote:
Originally Posted by
andreas90
Code java:
try {
//code that may throw the exception, the parsing method
} catch (ExceptionThatMayBeThrown e) {
e.getStackTrace();
return INVALID;
}
To understand which return statement is executed (since
INVALID will be returned in multiple cases) I would also suggest you print a message before you return anything .
Getting there I think. This is my code now (I'm confused about the "exceptionthatmaybethrown")
Code :
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 );
try {
if ( selection >= lower && selection <= upper ) {
return selection;
}
} catch ( java.util.InputMismatchException e ) {
e.getStackTrace ( );
return INVALID;
}
}
Now I am getting an error that this needs to return something, which it does, so somehow the returns are not being reached.
Sorry, this is the first time I have attempted error handling in Java. (other than a few simple if statements in previous works.)
Regards,
Max
EDIT: Updated code (I think I am getting closer) (but eclipse is still saying I need a return statement)
Code :
private int getChoice ( int lower, int upper ) {
System.out.println ( "--------------------------------------------------------------" );
System.out.print ( "Please enter your choice: ");
String stringSelection = Keyboard.readInput ( );
System.out.println ( "--------------------------------------------------------------" );
try {
int selection = Integer.parseInt ( stringSelection );
if ( selection >= lower && selection <= upper ) {
return selection;
}
} catch ( java.util.InputMismatchException e ) {
return INVALID;
}
}
Re: User selection problem
Quote:
Originally Posted by
zipstacrack
Getting there I think. This is my code now (I'm confused about the "exceptionthatmaybethrown")
"exceptionthatmaybethrown" was just fordemonstration. You should catch the exception that actually may be thrown which in your case is the NumberFormatException.
The try block should contain the statement that may throw the exception. In your code the exception will may be thrown from this statement
Code java:
int selection = Integer.parseInt ( stringSelection );
For example if the user enters a letter the method won't parse it to an int since it doesn't have the appropriate format (see the details in the above link). The inner if...else block won't throw an exception so it doesn't need to be indide the try block.
You should read the java tutorials for exceptions handling.
Re: User selection problem
Quote:
Originally Posted by
zipstacrack
EDIT: Updated code (I think I am getting closer) (but eclipse is still saying I need a return statement)
It "complains" becaused you took away the else block and therefore there is case that the method will not return anything. For example if the selection is out of the entered bounds.
Re: User selection problem
Quote:
Originally Posted by
andreas90
"exceptionthatmaybethrown" was just fordemonstration. You should catch the exception that actually may be thrown which in your case is the
NumberFormatException.
The try block should contain the statement that may throw the exception. In your code the exception will may be thrown from this statement
Code java:
int selection = Integer.parseInt ( stringSelection );
For example if the user enters a letter the method won't parse it to an int since it doesn't have the appropriate format (see the details in the above link). The inner
if...else block won't throw an exception so it doesn't need to be indide the
try block.
You should read the
java tutorials for exceptions handling.
If I was to not include the if statement in the try statement how would I check if the integers were between the lower and upper local variables?
It still requires a return type apparently.
I really appreciate your help, thanks,
Max
EDIT: I GOT IT! I always feel so good once a problem that I've been working on for a long time is solved.
Here is my updated code if anyone finds it useful:
Code :
private int getChoice ( int lower, int upper ) {
System.out.println ( "--------------------------------------------------------------" );
System.out.print ( "Please enter your choice: ");
String stringSelection = Keyboard.readInput ( );
System.out.println ( "--------------------------------------------------------------" );
try {
int selection = Integer.parseInt ( stringSelection );
if ( selection >= lower && selection <= upper ) {
return selection;
} else {
return INVALID;
}
} catch ( NumberFormatException e ) {
e.getStackTrace();
return INVALID;
}
}
Thank you very much for your help :)
Regards and out,
Max
Re: User selection problem
Glad you got it work.
Just for the record, you can take the input evaluation (if...else block) outside the try...catch block since in the latter you are just checking for the exception.
Re: User selection problem
Done. Thank you very much.