简体   繁体   中英

how can I use StringTokenizer with my jTable

I have a jTable and I want to display in it a result of a command,I used an object of the StringTokenizer class to separate the result pf the command cmd and display it in the table jTable1,my problem is when I use system.out.println(st.nextToken()) it works correctly but when I use data [0][i] to display the result in the table it displays when I compile the last result of the command in all lines of the table this is my code:

 public Object[][] data;
        public String  title[] = {"t1","t2","t3","t4","t5"};
       private void init_tab() {

            data = new Object[5][5];
                 for(int i=0;i<5;i++){
           try{ 
            String      cmd = "the command "                        
            ..... //command cmd traitment
          }
I
            String response = build.toString();  
             StringTokenizer st = new StringTokenizer(response,":"); 
               while (st.hasMoreTokens()) { 

                   //System.out.println(st.nextToken()) ; 


                   data[i][0]= st.nextToken();


           }
            }catch(Exception e){
        e.printStackTrace();}  

           jTable1= new JTable(data, title);
            jScrollPane1.setViewportView(jTable1);
                     }

             }

can someone help me I know the problem in data[i][0] but how can I solve this problem and thanks

The reason it is displaying last result of a command is you have coded it like that. Please have a look at your, following part of a program

      String response = build.toString();  
         StringTokenizer st = new StringTokenizer(response,":"); 
           while (st.hasMoreTokens()) { 
               //System.out.println(st.nextToken()) ; 
               data[i][0]= st.nextToken();
       }

You are iterating over all string tokens separated by, ":" and adding it at same location data[i][0]. As you are adding it to same index in data(rows) array, it will iterate through all the tokens and each time it will replace the token added previously. This will happen till StringTokenizer has more tokens and will come out of above loop after adding last token. Please fix this as follows if you want to display 5 tokens in 5 different columns,

         String response = build.toString();  
         StringTokenizer st = new StringTokenizer(response,":"); 
         int j = 0;
          while (st.hasMoreTokens()) { 
               //System.out.println(st.nextToken()) ; 
               data[i][j]= st.nextToken();
               j++;
           }

For all practical purposes, using StringTokenizer is considered deprecated . It's only there for backwards compatibility, and nowadays the preferred way to achieve the same result is to use the split() method in the String class, like this:

String response = build.toString();  
String[] st = response.split(":");
for (int j = 0; j < st.length; j++) {
    data[i][j] = st[j];
}

You may also be interested in using the Splitter class from the Google Guava library, eg Iterable< String > tokens = Splitter.on( ":" ).split(); for( String token : tokens ) { // do something } It is immutable and has a nice fluent API.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM