954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Using split with a line break

Hi,

I'm trying to figure out how can I make split read from the file and recognize how to put the strings into an array from a file, based on a new line break.

Example of my txt file:

This will be my first huge paragraph, it will contain many different characters such as numbers (123...) periods and etc...

My second paragraph, once again pretty big. Each paragraph consisting of about 50 - 150 characters.

Third paragraph, ans so on. Looking for an aim for atleast 50+ paragraphs.

Please suggest what would be the best and simple method to approach this. Thanks!

kashn
Light Poster
27 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

There are many examples on how to read files in this forum. After you read each line, check if it has value an empty String:

if (line.trim().equals("")) {
 // this is an empty line
 // the new line would be the start of a new paragraph
}
javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 
public void split() {
      String s3 = "what ever the text you wish to have with new lines";
      String [] temp_store= null; // array to store the separated string
      temp_store = s3.split("/n"); // /n is the new line and it will get divide from there and get saved in the array
      
    // have a FOR or a WHILE loop and print it
  }
localp
Junior Poster
191 posts since Apr 2008
Reputation Points: 1
Solved Threads: 3
 
public void split() {
      String s3 = "what ever the text you wish to have with new lines";
      String [] temp_store= null; // array to store the separated string
      temp_store = s3.split("/n"); // /n is the new line and it will get divide from there and get saved in the array
      
    // have a FOR or a WHILE loop and print it
  }

It will not work because the 's3' variable doesn't have the the '\n' character.
And it depends on the way you read the file. The most common way is to read it line by line so you will never have the '\n' in the String read. With every loop you will have the next. What is required (if i understood correctly) is to find a way to separate the paragraphs.
So like I said whenever you read an empty line you have a new paragraph. It depends on how you want to save the lines read

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You