0

i want to make a JTable which should be used like a adressbook. so there is a button which is used to create new entrys. I want to create these entrys by using JOptionPane InpuDialog because I want that only Entrys with at least name and age are created. Also i want to Check that there are no duplicates (same name). So i post you the code of my add-button. The check if the age ist between 1 and 100 works correct but i forget to check if the selected name is really not a duplicate, if you click the ok-button wo times he accept duplicates. So my question is if it is applicable to realize the check with i while loop ( like i did down bellow) or is there an easier way to realize it?

                      boolean notAllowed=true;
                boolean noCreation=false;
                DefaultTableModel dtm = (DefaultTableModel) table
                        .getModel();
                String s = JOptionPane.showInputDialog("Select Name");
                for (int i = 0; i < dtm.getRowCount(); i++) {
                    if (s.equals(dtm.getValueAt(i, 0))) {

                        s = JOptionPane.showInputDialog("Selected Name already in use \n Select an other Name");




                        i = 0;
                    }
                }
                String c = JOptionPane
                        .showInputDialog("age?",JOptionPane.OK_OPTION);
                while(notAllowed){

                try{

                    int teste =Integer.parseInt(c);
                    if(teste==JOptionPane.CANCEL_OPTION)
                    {
                        notAllowed=false;
                        noCreation=true;
                    }
                    if(teste<=100 && teste>0 &&notAllowed)
                        notAllowed=false;




                }
                catch( Exception err)
                {
                    notAllowed=false;
                    noCreation=true;
                }
                if(notAllowed)
                c = JOptionPane.showInputDialog("Only Numbers between 1 and 100 are allowed");
                }
                if(!noCreation)
                {

                                     //create Entry
                                    }
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Jaran
  • 212
  • 2
  • 10

1 Answers1

0

my recommendation is create a File named validation in a package named controller in the validation file create a class Validation whit two methods one for check the names and other for check the range of ages and then called this methods, this is for re-use your code and simplified also.

Premier
  • 766
  • 2
  • 10
  • 20