dimanche 7 août 2016

validate input using try catch

I am trying to validate input text field in java by creating an exceptional error if user does not type in the word specified i.e. admin

        String username;
        int phone1;

        @Override
        public void widgetSelected(SelectionEvent e) {          
            try
            {
            phone1 = Integer.parseInt(phone.getText());
            }
            catch (Exception exe)
            {           
                MessageDialog.openError(shell, "Error","Phone number has to be a number");
                return;
            }

            try
            {
            username = namefield.getText();
               if (username!= "admin") {
                    throw (new Exception());
                }           
            }
            catch (Exception exe)
            {   
                MessageDialog.openError(shell, "Error","The username must be admin");
                return;
            }


            labelresult.setText("The name entered is:"+ username );


            labelresult2.setText("The phone entered is:"+ phone1 );

The one for phone works perfectly

What about for username. What should i do?

Aucun commentaire:

Enregistrer un commentaire