Search code examples
javaswingjoptionpane

JOptionPane InputMismatchException


i am building a simple chat application and i'm thinking to set the server address and port through JOptionPane, what i want to do is to check that the user is not entering letters in when setting up the port number,

int port=Integer.parseInt(JOptionPane.showInputDialog("Please insert the default port!");

i tried to handle it with a try/catch block but it didn't worked, any suggestions?


Solution

  • NumberFormatException is apparently the answer, so now this question will not show up as unanswered.

    try{
        int port=Integer.parseInt(JOptionPane.showInputDialog("Please insert the default port!");
    }catch(NumberFormatException e){
       //handle error
    }