Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / October 2004

Tip: Looking for answers? Try searching our database.

JTable - parsing from file

Thread view: 
Chris - 04 Oct 2004 15:42 GMT
Hello everyone.

I would be most grateful if someone could take the time to have a
brief look at my problem.

I am currently trying to read from a file with a view to populating a
jtable.  I try to read in a line, tokenise it, add each token to a
vector and then add this vector to the table as a row.

However, for some reason the first cell in each row contains the same
address as all the other rows. This address is the last address in the
file. The number of rows created is correct.   I tried playing around
with clearing the vector each time, but it started throwing an
ArrayOutofBoundsException.

I am sorry I cannot provide the whole program.  It is very large.

Any help would be much appreciated.  I am hoping it is something
obvious which I am missing.

My data file:

65.43.23.4:34
65.43.23.4:54
61.43.23.4:23
65.23.23.4:65
25.43.23.4:98
15.33.23.4:21
95.43.23.4:8080
75.43.23.4:8080
65.3.23.4:8080
65.43.23.4:8080
65.43.3.4:8080
63.43.23.4:8080
98.65.4.3:1040
75.45.32.3:4056
32.34.4.3:456

My code :

try {
    Vector addyIn = new Vector();
    StringTokenizer tokens;
    String line;
    String token = null;
    FileInputStream inFile = new FileInputStream(in);
    BufferedReader input = new BufferedReader (new
InputStreamReader(inFile));
       
    int tokenNo;
       
            //Read in line from file
    while ((line = input.readLine()) != null ) {

        //get tokens
        tokens = new StringTokenizer(line," ");

            // loop through tokens adding each on to a vector.
            for ( int i = 0 ; i < tokens.countTokens ; i ++) {
            token = tokens.nextToken();
            addyIn.addElement(token);
            }
           
            // Add Vector as a row to the datamodel.

            dataModel.addRow(addyIn);
            //addyIn.clear();           
                        }
           
         
       
                    }
                      catch (Exception e) {
                        e.printStackTrace();
                        }
Rogan Dawes - 04 Oct 2004 16:05 GMT
> Hello everyone.
>
[quoted text clipped - 15 lines]
> Any help would be much appreciated.  I am hoping it is something
> obvious which I am missing.

It could be something quite obvious that you are missing. Most likely,
it is that you are reusing the SAME vector for each row?

Also, your row vectors all need to be the same length, otherwise, you
will most likely get ArrayOutOfBoundsExceptions thrown (which you
obviously did see).

I suggest you start by creating a new vector for each row. Then, once
you have tokenised your line into the vector, print out the size of the
vector before adding it to the table. You will probably spot the error
before too long . . .

Rogan

Signature

Rogan Dawes

*ALL* messages to discard@dawes.za.net will be dropped, and added
to my blacklist. Please respond to "nntp AT dawes DOT za DOT net"

Paul Lutus - 04 Oct 2004 16:23 GMT
> Hello everyone.
>
[quoted text clipped - 7 lines]
> However, for some reason the first cell in each row contains the same
> address as all the other rows.

This is because you are trying to reuse the same Vector object. You must
create a new Vector object for each row.

Move the following line of code:

> Vector addyIn = new Vector();

Inside your loop. Do not true to use Vector.clear(), instead create a new
vector.

Signature

Paul Lutus
http://www.arachnoid.com

Chris - 05 Oct 2004 09:51 GMT
> > Hello everyone.
> >
[quoted text clipped - 17 lines]
> Inside your loop. Do not true to use Vector.clear(), instead create a new
> vector.

Thanks guys.

As always, you have been a great help.

Much appreciated.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.