Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Finding how many lines are in a text file?
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
JayLo




PostPosted: Wed Dec 15, 2004 11:01 pm   Post subject: 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?
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Wed Dec 15, 2004 11:02 pm   Post subject: (No subject)

Vectors resize themselves, so you can just keep on feeding data into one.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
JayLo




PostPosted: Wed Dec 15, 2004 11:08 pm   Post subject: (No subject)

Yeah, Vectors seem to be better.
The fileStream still isn't reading in all the lines of text in the file.
code:

        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




PostPosted: Wed Dec 15, 2004 11:30 pm   Post subject: (No subject)

why are there 4 different vectors?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
wtd




PostPosted: Wed Dec 15, 2004 11:50 pm   Post subject: (No subject)

Keep different things different! Read the file in in one place, and tokenize the various strings in another.
Martin




PostPosted: Wed Dec 15, 2004 11:55 pm   Post subject: (No subject)

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




PostPosted: Thu Dec 16, 2004 9:09 am   Post subject: (No subject)

martin wrote:
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




PostPosted: Thu Dec 16, 2004 6:22 pm   Post subject: (No subject)

well in programming you can safely assume that the user is intellectually challanged and will actually try to break your program Wink
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: