wood burning stoves*
The moose likes I/O and Streams and the fly likes print till the next blank line Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "print till the next blank line" Watch "print till the next blank line" New topic
Author

print till the next blank line

Mah pish
Greenhorn

Joined: May 12, 2011
Posts: 7
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

Joined: Oct 01, 2001
Posts: 2848
    
  11

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

Joined: May 12, 2011
Posts: 16

Hi,

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


Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2848
    
  11

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

Joined: May 12, 2011
Posts: 7
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

Joined: Oct 01, 2001
Posts: 2848
    
  11

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: print till the next blank line