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
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: 672
posted
0
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: 32833
4
posted
0
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.
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: 672
posted
0
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: 32833
4
posted
0
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).
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.
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