0

This is code for reading a text field in Java Swing

jbtLogin.addActionListener(new ActionListener() {
    String id=jtfId.getText();      

    String s=new String(jpass.getPassword());
    int password = Integer.parseInt(s); 

But I have this error:

Exception in thread "main" java.lang.NumberFormatException: For input     string: ""
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
Andrew Thompson
  • 163,965
  • 36
  • 203
  • 405
Leila
  • 21
  • 1
  • 4
  • 1
    What is your question? – IQV Feb 16 '17 at 06:59
  • your string might be empty or contains characters other than digits. – jack jay Feb 16 '17 at 07:00
  • 1
    The error says that you can't convert an empty string to an `int`. You have to check this before. – IQV Feb 16 '17 at 07:00
  • 1
    Possible duplicate of [java.lang.NumberFormatException: For input string: ""](http://stackoverflow.com/questions/3701520/java-lang-numberformatexception-for-input-string) – msagala25 Feb 16 '17 at 07:02
  • also check this, to check if your input is numeric. http://stackoverflow.com/questions/1102891/how-to-check-if-a-string-is-numeric-in-java/1102916#1102916 – msagala25 Feb 16 '17 at 07:05
  • 1) `String s=new String(jpass.getPassword());` A password should never be turned into a `String` as it becomes a security weakness. 2) And `int password = Integer.parseInt(s);` allowing 'only integers' in a password is another security weakness. In general, passwords should enforce that they contain numeric, lower ***&*** upper case letters. – Andrew Thompson Feb 16 '17 at 07:11
  • [This is why you shouldn't convert the `char[]` value return by `JPasswordField` to a `String`](http://stackoverflow.com/questions/8881291/why-is-char-preferred-over-string-for-passwords) - this real question is, why are you expecting an `int`? – MadProgrammer Feb 16 '17 at 07:56

0 Answers0