I know this question has been asked a few times, and I've tried what was suggested in the responses, but it's not working for my particular situation.
This is a school assignment FYI.
I'm writing a simple method to check if the user entered a numerical value with a try/catch block. The problem is, my professor grades down for unused variables, which makes sense, and I can't figure out any way to use the userInputValue variable. I tried using it in my MessageDialog but since it was declared inside the Try block, it couldn't access it. I tried moving it outside of the block, but then that variable went unused. Is there any way I can reword this but retain the same function while not having that unused variable?
public boolean numericalUserInput(String userInput){
try {
double userInputValue = Double.parseDouble(userInput);
}
catch(NumberFormatException notANumber){
JOptionPane.showMessageDialog(null, "You entered " + userInput + " but you should only enter numbers, please try again.");
userEntryTextField.setText("");
return false;
}
return true;
}
Thanks!