aspose file tools*
The moose likes Beginning Java and the fly likes Line Counting in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Line Counting in Java" Watch "Line Counting in Java" New topic
Author

Line Counting in Java

Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 740
I am trying to count the lines of the news for the purpose to calculate that how many lines translated by a specific operator but its not working accurately. Can you please guide me where I am wrong

Thanks & best regards




Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
Remember that line end doesn't mean "\n". There is a list of line end characters in the Pattern class. It may require two characters to terminate a line.

If you have managed to split the lines, you have a count which is the length attribute of the array.

Please be more specific about what errors you are experiencing; just saying "not working accurately" doesn't give us much to work on.
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 740
Dear Campbell Ritchie,

Thanks for your reply

Sorry to not explain in detail.

1. I am getting "news" from oracle XE
2. I was thinking that for counting the lines there may be three scenarios. (a) There must be empty lines
(b) There must be new lines e.g. user pressed two times inter for new para
(c) There must be Carriage return

All of these is shown by my Java Servlet so I made a simple method to get the column News and start checking that how many empty lines + new lines + carriage returns and on these basis I tried to write this code.

Now the code is not throwing any error and working but not counting the lines as I was expecting keeping the above points

Thanks & best regards
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
Not sure. Try splitting up the problem. First see if you can create a regex for the line end combinations and count how many times it matches.

Anybody else able to help, please?
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
You shouldn't think about carriage returns and line breaks.

Use


the short coding version:

1] split the text into an array of lines, split on newline
2] test for length 0, optionally using String.trim() to clean leading/trailing white space


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 740
Originally posted by Bill Shirley:
the short coding version:

1] split the text into an array of lines, split on newline
2] test for length 0, optionally using String.trim() to clean leading/trailing white space[/QB]




I tried in the above way what I understand from your directions but not giving the correct counting of lines. Please correct me if I am wrong

Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
Thank you, Bill. Far better suggestion than mine.

You are testing words.length when what you actually want is the length of the member of words. Suggest a for-each (enhanced for) loop, and you check the length of the individual String (trimmed or otherwise).
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 740
Dear Bill Shirley,

I am waiting for your reply

Thanks again
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42648
    
  65
Who knows if Bill will ever come back to this topic? But there's no need to wait for that, Campbell already told you what you need to do to fix the code.


Ping & DNS - my free Android networking tools app
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
Originally posted by Farakh khan:
Dear Bill Shirley,

I am waiting for your reply

Thanks again


Bill has replied.
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 740
Originally posted by Campbell Ritchie:


Bill has replied.


Sorry to say that I have problem with the Bill reply as its not working for me or may I understood wrong. Please correct me

Thanks & best regards
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
Please explain what is going wrong.
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 740
Originally posted by Campbell Ritchie:
Please explain what is going wrong.


I really appreciate your help

Yes this is the problem with me that I don't know where I am wrong. I provided the whole details + my code and request you again to please check it and favor to solve my problem

Thanks for your prompt replies

Thanks again & best regards
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Line Counting in Java