-2

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!

bnr32jason
  • 113
  • 1
  • 11
  • 4
    Why have the variable at all? Just call `Double.parseDouble(userInput);` and be done with it. – azurefrog Nov 15 '15 at 23:28
  • Put your code where you want to use `userInputValue` inside the try block – stonar96 Nov 15 '15 at 23:29
  • http://stackoverflow.com/questions/14206768/how-to-check-if-a-string-is-numeric – frostbite Nov 15 '15 at 23:29
  • @SteffenKreutz I'm not sure why, but it wasn't working that way before, I must have reformatted some other code at the same time. It works that way now. Thank you! – bnr32jason Nov 15 '15 at 23:32
  • @azurefrog it didn't work that way for some reason before, but now it is. I must have changed some other code I was playing around with. It works now, thanks! – bnr32jason Nov 15 '15 at 23:33

3 Answers3

1

Since you do not need the parsed number, you can simply omit the assignment:

public boolean numericalUserInput(String userInput){
    try {
        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;
}
stevecross
  • 5,588
  • 7
  • 47
  • 85
1

It seems you don't need to make use of userInputValue as you are only using this method to check whether the userInput string is numerical. You can just leave userInputValue away like this:

public boolean numericalUserInput(String userInput){

    try {
        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;
}
0

in your method you don't need a variable to carry the return value of parse double method

public boolean numericalUserInput(String userInput){

try {
    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;
}

if you need the returned number you can change your method to get use of the number as follow

public double numericalUserInput(String userInput){
     double userInputValue;
try {
    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 Double.NaN;
}
return userInputValue ;
}
Ahmed Mazher
  • 323
  • 3
  • 7