Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How can I find out how many lines there are?

 
ben riches
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,
I have this code that reads in a file checks for commas and then adds all the appropiate text to a vector.


But how can find out how many lines of text there is in my file?
for example say these 2 lines where the the 2 lines in my work.csv file, I want to be able to get the output as 2.

Thankis
Lol
[ January 05, 2004: Message edited by: ben riches ]
 
Dale DeMott
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may be able to search for \n or \r depending on what your data file is using. That should tell you how many lines there are. Example... Number of lines = number of \r + 1
-Dale
 
Gillian Bladen-Clark
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The StringTokenizer is designed to do all the work of deciding what is a word separator for you - in your case, a comma.
I suggest you read line by line then get the tokens. e.g.

And the code for the file reading class is:

[ January 05, 2004: Message edited by: Gillian Bladen-Clark ]
 
John Ipe
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think counting the number of times buff.readLine() returns a non-null value will tell you how many lines there are, cos readLine reads the file one line at a time. So all you may need is a counter that counts how many times your readLine executed without returning null.
 
Charlie Havener
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the excellent Ostermiller utilities to read csv files, even the sicko Microsoft Excel csv files.
Ostermiller Utilities
Check it out. It is the greatest thing since sliced bread. It works like this.
FileReader in = new FileReader( sourceFile);
ExcelCSVParser parser = new ExcelCSVParser(in);
String[][] values = parser.getAllValues(); // values[ rows ][ columns ]
int numCols = values[0].length;
int numRows = values.length;
The numRows will be your number of lines.
Charlie www.cdhconsult.com
 
Karthik Veeramani
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good link man! Those Ostermiller utils look real cool!! thanks
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you could simply use the java.io.LineNumberReader
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:
Or you could simply use the java.io.LineNumberReader

In addition, I would like to mention that using an integrated class results better performance than using a third-party package like Ostermiller utilities... but I'm not sure whether there are good features added in that utilities... If so, just go ahead to use that third-party package... Otherwise consider about the performance of ur application as well...
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ko Ko Naing:

In addition, I would like to mention that using an integrated class results better performance than using a third-party package like Ostermiller utilities...

Why would that be???
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:

Why would that be???

I mean I support people, who use the ready-made classes like java.io.LineNumberReader, rather than re-inventing the wheel or using the re-invented wheel in the third-party packages...
Coz Sun also recommend developers to use the classes in the Java Standard API, instead of inventing the same thing, especially when we take the SCJD exam... They extract the marks, if we are trying to re-invent the wheel...
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also think that reinventing the wheel is stupid, and using existing libraries will certainly improve development performance.
I somehow did understand you to mean that using Sun libraries could somehow improve *application* performance. Sorry if I misunderstood you.
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:
I somehow did understand you to mean that using Sun libraries could somehow improve *application* performance. Sorry if I misunderstood you.

It's ok. Sometimes I can feel that my language usage is not clear enough to make other people understand well what I want to express....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic