• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Line Counting in Java

 
Ranch Hand
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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




 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Farakh khan
Ranch Hand
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Bill Shirley,

I am waiting for your reply

Thanks again
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Farakh khan:
Dear Bill Shirley,

I am waiting for your reply

Thanks again



Bill has replied.
 
Farakh khan
Ranch Hand
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please explain what is going wrong.
 
Farakh khan
Ranch Hand
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic