1

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

mKorbel
  • 109,525
  • 20
  • 134
  • 319
hanem
  • 101
  • 1
  • 2
  • 7
  • 2
    1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) Please use a consistent and logical indent for code blocks. 3) Please learn common [Java naming conventions](http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#73307) for class, method & attribute names & use it consistently. One specific example is that `init_tab()` should be `initTab()` – Andrew Thompson Mar 18 '12 at 12:24
  • ... as difficult as reading the code: please fix the formatting :-) – kleopatra Mar 18 '12 at 12:25
  • thanks deporter,the jTable should returns the resulta of the command that I taped,then I used StringTokenizer to separate the result and made it in the jTable,but when I compile with this code,the same line (the last result of the cmd command) repeating in all lines of the jTable – hanem Mar 18 '12 at 12:27

3 Answers3

2

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++;
           }
Rahul Borkar
  • 2,742
  • 3
  • 24
  • 38
1

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];
}
Community
  • 1
  • 1
Óscar López
  • 232,561
  • 37
  • 312
  • 386
0

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

nicktalbot
  • 416
  • 2
  • 6
  • thanks for your replys,but I want to separte the String result with the split method then every line of the first column should have the result of separation how can I increment with the next line and thanks – hanem Mar 19 '12 at 01:06