
-----------------------------------
JayLo
Wed Dec 15, 2004 11:01 pm

Finding how many lines are in a text file?
-----------------------------------
I'm trying to find out how many lines are in a text file, so i know how big my array should be. Actually, is there a more efficient way of getting data, casting it into an Array/Vector with the correct dimensions?

-----------------------------------
Tony
Wed Dec 15, 2004 11:02 pm


-----------------------------------
Vectors resize themselves, so you can just keep on feeding data into one.

-----------------------------------
JayLo
Wed Dec 15, 2004 11:08 pm


-----------------------------------
Yeah, Vectors seem to be better.
The fileStream still isn't reading in all the lines of text in the file.

        while ((c=in.read())!=-1)
        {
            line = input.readLine ();
            st = new StringTokenizer (line);
            while (st.hasMoreTokens ())
            {
                a.add(st.nextToken());
                b.add(st.nextToken());
                c.add(st.nextToken());
                d.add(st.nextToken());               
            }
        }

where a, b, c, d are Vectors

-----------------------------------
Tony
Wed Dec 15, 2004 11:30 pm


-----------------------------------
why are there 4 different vectors?

-----------------------------------
wtd
Wed Dec 15, 2004 11:50 pm


-----------------------------------
Keep different things different!  Read the file in in one place, and tokenize the various strings in another.

-----------------------------------
Martin
Wed Dec 15, 2004 11:55 pm


-----------------------------------
If the file is just a square of text, you could find out how big one line is, compare that to the size of the text file, and calculate the number of lines.

-----------------------------------
wtd
Thu Dec 16, 2004 9:09 am


-----------------------------------
If the file is just a square of text, you could find out how big one line is, compare that to the size of the text file, and calculate the number of lines.

You know what they say about assuming...

-----------------------------------
Tony
Thu Dec 16, 2004 6:22 pm


-----------------------------------
well in programming you can safely assume that the user is intellectually challanged and will actually try to break your program :wink:
