dimanche 29 mai 2016

if/else statement based on comma count

I am loading text file contents to GUI using this code. I use String.split() method to split the line. Now by counting commas in some line I want to set them to jComboBoxes and jTextFields. I tried to use if/else statement to switch condition for jComboBoxes and jTextFields in below code. But when I used if/else for 2 cases, that is commas<4 and commas<3, I found that jComboBoxes and jTextFields are interfere each other. In case of when commas==3, jTextField1 getting the value of t1[1] except t1[2].

I would like to ask how can I write if/else for this case without interfering each other (jComboBoxes and jTextFields)?

    String[] t1 = authors.toString().split(",");

      int commas = 0;
  for(int i = 0; i < authors.toString().length(); i++) {
     if(authors.toString().charAt(i) == ',') commas++;

   if(commas<4){
     jcb1.setSelectedItem(t1[0]);

     jTextField1.setText(t1[1]);

     jTextField2.setText(t1[4]);
 }
   else if(commas<3){
     jcb1.setSelectedItem(t1[0]);

     jTextField1.setText(t1[2]);

     jTextField2.setText(t1[3]);        
 }                  
 }
      System.out.println(commas);

Aucun commentaire:

Enregistrer un commentaire