Search code examples
javajtextfieldnumberformatexception

NumberFormatException when converting a textfiled input to integer


I have a JTextFiled. and i will be typing some integer values in to it.

I have another method called getMyAge(int age) which requires a int as a parameter.

I end up having a NumberFormatException, when i try to pass the integer that i typed in the textfield.

                    getMyAge(Integer.parseInt(txtFiled.getText()));

How can i fix this ? (I will at all instances type a Number in to the textfield). I tried the try-catch block but it still doesn't work.


Solution

  • Can you try printing the content of txtFiled.getText() maybe you can try replacing it by txtFiled.getText().trim() because an extra empty space can easily cause the problem.