0

I am trying to change the value in a jTextField when I press the UP and DOWN arrow keys on the keyboard. When UP is pressed the value goes up by a set amount and when DOWN is pressed it goes down.

This is what I so far:

private void engineTxtKeyPressed(java.awt.event.KeyEvent evt) {                                 

    try{
    int code = evt.getKeyCode();
    double value = Double.parseDouble(engineTxt.getText());
    if (code == KeyEvent.VK_UP) {
        value = value + 25.00;
        if ((value) > 100.00) {
            value = 100.00;
            engineTxt.setText(String.format("%.2f", value));
        }
    }
    else if(code == KeyEvent.VK_DOWN){
        value = value - 25.00;
        if ((value) < 0.00) {
            value = 0.00;
            engineTxt.setText(String.format("%.2f", value));
        }
    }
    } catch(Exception e){
        System.out.println("error");
    }
}  

Before I put the try/catch in there, the error I got was:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "0,00"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)

The list goes all the way down for many lines, basically it would flag anything with parsing and formats throughout the process.

The thing is that I have the same parsing for another 10 TextFields and there is no problem, but this is the only one with a keyEvent. After adding the try/catch I get my sout() message every time i press a key and the rest of the program continues just fine.

user89910
  • 53
  • 1
  • 1
  • 7
  • Looks like your problem is the parseDouble() method, not the keyboard events. – DaaaahWhoosh Feb 03 '15 at 15:04
  • In this case the input probably was `0,00` (from error log) for which you will get `NumberFormatException` while parsing. Did you try with other values? – Minar Mahmud Feb 03 '15 at 15:06
  • Possible duplicate of [parseDouble in Java results to NumberFormatException](http://stackoverflow.com/questions/8285982/parsedouble-in-java-results-to-numberformatexception). – Iootu Feb 03 '15 at 15:08
  • Other values meaning without `,` (comma), just plain numbers. – Minar Mahmud Feb 03 '15 at 15:09
  • those values in the textfield are put by a double Random. When i parse them in other textfields it works. I even tried with a simple "0" but the error pops whenever i press the key. And it even pops when I press a different key other than UP or DOWN – user89910 Feb 03 '15 at 15:12
  • If it is so, then I am afraid there is no way to suggest anything without seeing the full code. I think despite using Random, *somehow* the textfield is getting set with value containing `,`. But cant tell surely without seeing the code. – Minar Mahmud Feb 03 '15 at 16:17

1 Answers1

0

When you get to the line :

double value = Double.parseDouble(engineTxt.getText());

The string engineTxt.getText() returns (String) 0,00, which raises an error.

While

double value = Double.parseDouble("0");
System.out.println(value);

Wont raise an exception,

double value = Double.parseDouble("0,00");
System.out.println(value);

does.

Somewhere along the line, there is a , that should'nt be there. Since this will work:

double value = Double.parseDouble("0.05");
System.out.println(value);

EDIT:

Like someone said in the comments, while the OP is not aware of it, it is a Possible duplicate of parseDouble in Java results to NumberFormatException

Community
  • 1
  • 1
Raphaël
  • 173
  • 11