File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

print till the next blank line

 
Mah pish
Greenhorn
Posts: 7
  • 0
  • 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
 
Greg Charles
Sheriff
Posts: 2984
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • 0
  • 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.
 
Mykhailo Kozik
Greenhorn
Posts: 16
Eclipse IDE Java Ubuntu
  • 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: 2984
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • 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
  • 0
  • 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: 2984
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • 0
  • 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