• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

print till the next blank line

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I am writing a code that would take a string which has blank lines between sentences. i need to find a certain word and print out all the next words following it till it reaches a blank line. for instance: test1 as the input in the following string:

test0

test1

test2 test3

test4


and returning: test2 test3


My code: ( text is the string )


any help would be appreciated
 
Sheriff
Posts: 3034
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch! I added code tags to your post to make it easier to read.

In th line p = text.indexOf(intIndex,old_p), both intIndex and old_p are defined as ints, but there is no String.indexOf() method that works on two ints. What were you going for there? I would think you'd want to look for a line separator.
 
Greenhorn
Posts: 16
Eclipse IDE Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I understood that blank line means two returns (\n\n)
i think better solution to use StringTokenizer:


 
Greg Charles
Sheriff
Posts: 3034
12
Mac IntelliJ IDE Python VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line separators are platform specific though. You'd have to know what the String was using. I'd actually prefer a StringReader, wrapped in a LineNumberReader for this job, but a StringTokenizer could work. So could just searching for line separators.
 
Mah pish
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies, But i still cant work it out. I gave up the other method, Now i am trying to find the word that comes between the other words (test1 and test 5). any help?
 
Greg Charles
Sheriff
Posts: 3034
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, in that case, you know how to get the index of "test1" and the index of "test5", and you know how to get a substring. It seems like you're done. If you need to break the substring into words, then something like substring.split("\\s") should do the job for you.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!