-1

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?

xhulio
  • 1,093
  • 1
  • 13
  • 32

1 Answers1

1

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
}
Abu Sulaiman
  • 1,477
  • 2
  • 18
  • 32
  • you know of any way to make the JOptionPane show up until you put the right result?, i mean if you put a letter, prompt an error message and than reenter the input again? – xhulio Apr 16 '14 at 21:05
  • 1
    I have used DocumentFilter for this. It works very well. Here is a link to an example:http://stackoverflow.com/questions/11093326/restricting-jtextfield-input-to-integers – Abu Sulaiman Apr 16 '14 at 23:11