Results 1 to 5 of 5
  1. #1
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default JTable, Vector, File, Problem.

    <SOLVED>


    Evening all. Have a small problem. Still trying to understand JTable. Problem is that I have 1 file named row.txt. The code below is for reading that file and put it in vector row. when i do System.out.print(row) it prints out all of it. But in my table it only showing first row in the file. ex if file contain.

    row1 row2 row3
    row4 row5 row6

    Table only showing row1 row2 row2. if i add more cols. they appear. what have i missed?

    Java Code:
    public class RowClass extends DefaultTableModel {
         File file = new File("c://java/row.txt");
         FileInputStream fis = null;
         BufferedInputStream bis = null;
         DataInputStream dis = null;
         int count;
         String str;
         String[] arr;
             
    void rowVoid() {
          
       try{
          fis = new FileInputStream(file);
          bis = new BufferedInputStream(fis);
          dis = new DataInputStream(bis);
          while ((str = dis.readLine()) !=null ){
          arr = str.split(" ");
          count++;
          for (int i = 0; i < arr.length; i++){
           row.addElement(arr [i]);
           System.out.println(arr [i]);
            }
          }
          for (int i = 0; i < count; i++) {
          rowData.addElement(row);
            }
          fis.close();
          bis.close();
          dis.close();
          }catch  (IOException e) {
          e.getStackTrace();    
          System.out.println("Error" +e.toString() );
           }
        }
     }
    Last edited by ocean; 11-06-2009 at 07:56 AM.

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    You need to create a new "row" for every line of data in the file.

  3. #3
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default

    That´s what i thougth i did. with the rowData loop. Would be happy if someone could point out what i do wrong.

    Java Code:
        Vector<Vector> rowData = new Vector<Vector>();
        Vector<String> col = new Vector<String>();
        Vector<String> row = new Vector<String>();

    And the loops do

    Java Code:
    while ((str = dis.readLine()) !=null ){
    
          arr = str.split(" ");
          count++; 
    
          for (int i = 0; i < arr.length; i++){
           row.addElement(arr [i]);
             }
          
    
         for (int i = 0; i < count; i++) {
          rowData.add(row);
          }

    I dreamt a nigthmare about a vector that chased me tonight. Think i have overworked my small brain and need a break. :D

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    How many times does

    Java Code:
    row = new Vector<String>()
    appear in your code?

  5. #5
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default

    Oo it were that easy. Big thanks to you camickr.

    Java Code:
    while ((str = dis.readLine()) !=null ){
          arr = str.split(" ");
          row = new Vector<String>();
          for (int i = 0; i < arr.length; i++){   
              row.addElement(arr [i]);
          } 
         rowData.add(row);
          }

    And now it works as i like. This forum is amasing. Thank´s for having patient with me.

Similar Threads

  1. Problem with vector
    By morghul in forum New To Java
    Replies: 3
    Last Post: 11-04-2009, 02:24 AM
  2. Replies: 2
    Last Post: 05-15-2008, 10:15 AM
  3. How to Save/Load Vector to/from file
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:37 PM
  4. Vector problem
    By Ace_Of_John in forum New To Java
    Replies: 1
    Last Post: 01-27-2008, 08:53 PM
  5. vector problem
    By mambo_jumbo in forum New To Java
    Replies: 1
    Last Post: 11-17-2007, 10:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •