using the nextToken() method with the jTable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • norman2000
    New Member
    • Mar 2012
    • 3

    using the nextToken() method with the jTable

    hi,
    I have a jTable and I want to display in it a result of a command,then I used an object of the StringTokenizer class to separate between the lettres of the result and display the result in the table jTable1,but when I use system.out.prin tln(st.nextToke n()) it works correctly but in the table it display when I compile the last result
    this is my code:

    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
          }
            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);
                     }
                     
                 }
    I don't know where is the fault,can some one help me and thanks for help
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    In data[i][0]= st.nextToken(); you are just overwriting the same position with the same value all the time because the value of i never changes inside the while loop.

    Comment

    • norman2000
      New Member
      • Mar 2012
      • 3

      #3
      thanks r035198x for your reply but how can I solve this problem,make a new loop or what?

      Comment

      • rekedtechie
        New Member
        • Feb 2012
        • 51

        #4
        you dont need a while loop..
        put an if statement inside the for-loop to check if st.hasMoreToken ()
        then try to print st.nextToken catch error.
        Last edited by rekedtechie; Mar 19 '12, 12:45 AM. Reason: wrong interpretation.. :)) about for loop braces..

        Comment

        • rekedtechie
          New Member
          • Feb 2012
          • 51

          #5
          oops error..maybe its in your variable declaration.

          in programming we should declare all the variable first before we used it..

          but in this case..
          your declaraion is inside your loop..

          your code looks like this..
          Code:
          for(i=0;i<5;i++)
          {
          x=0;
          x++;
          System.out.println(x);
          }
          /*this code results
          1
          1
          1
          1
          1
          because we declare x = 0 we add 1 then we print the x..
          then loop again.. 
          x = 0 we add 1 then we print x..
          all the results will be x=1*/
          //try to put your StringTokenizer declaraion outside..

          Comment

          Working...