1

Very noobish questions but I want to read a file and have an array of paragraphs from that file. The paragraphs are separated by empty lines.

Example: String str = "Johnny likes to go outside but the weather has been so hot lately I don't think he'll be able to go unless his mother is cool with sunburns all over his body. Someone needs to help him understand that money doesn't grow on trees and unless you have sunscreen money then don't ask to go outside.

Melissa likes to go outside too but she wouldn't have anybody to play with because johnny's mom turned psycho. Kids get sunburned...that's just some kid stuff to do... why are you upset at that?";

Would be one string, if I used split() to make a String array I'd have 2 items in there because they are separated by an empty line...

class Main {
  public static String[] fileSearch(String fileName) throws IOException, NullPointerException {
    BufferedReader items = new BufferedReader(new FileReader(fileName));
    String line = items.readLine();
    while(line != null)
        { 
            //Where I want my file to be split by paragraph, I used "/n" but that's not what I really want. 
            String[] data = line.split("\n");
            line = items.readLine();
        }
    return data;
  }
  public static void main(String[] args) {
    fileSearch(info.txt);
  }
}
  • 2
    Could you split by two new line characters? line.split("\n\n") – jnorman Jul 18 '20 at 00:12
  • You have already split into lines because you use `readLine`. If you want to use regular expressions, you can read the entire file into one String. But you could also add lines to the current paragraph until you read an empty line with readLine, which would start a new paragraph. – Erwin Bolwidt Jul 18 '20 at 00:25
  • [See this SO answer](https://stackoverflow.com/a/26367165/4725875). – DevilsHnd - 退職した Jul 18 '20 at 04:01

2 Answers2

1

All you would need to do is detect the empty line. Meanwhile, append lines to build a paragraph.

Here is an example of how this may work:

        String str = "Johnny likes to go outside but the weather has been so hot lately I don't think he'll be able to " +
            "go unless his mother is cool with sunburns all over his body. Someone needs to help him understand that " +
            "money doesn't grow on trees and unless you have sunscreen money then don't ask to go outside. \n" +
            "\n" +
            "Melissa likes to go outside too but she wouldn't have anybody to play with because johnny's mom turned " +
            "psycho. Kids get sunburned...that's just some kid stuff to do... why are you upset at that?";
        String[] data = str.split("\n");
        ArrayList<String> paragraphs = new ArrayList<>();
        StringBuilder paragraph = new StringBuilder(100);
        for (int i = 0; i < data.length; i++) {
            String line = data[i];
            if (line.trim().isEmpty()) {
                System.out.println("New paragraph detected!");
                // this is your new paragraph indicator
                paragraphs.add(paragraph.toString());
                paragraph.setLength(0); // clear StringBuilder
            } else {
                // we are in an existing paragraph
                paragraph.append(line);
            }
            // check if we're at the end, we need to add last paragraph to results set
            if (i == data.length - 1) {
                paragraphs.add(paragraph.toString());
            }
        }
        System.out.println(paragraphs.toString());

Here is the output:

New paragraph detected!    
[    
    Johnny likes to go outside but the weather has been so hot lately I don't think he'll 
    be able to go unless his mother is cool with sunburns all over his body. Someone needs 
    to help him understand that money doesn't grow on trees and unless you have sunscreen 
    money then don't ask to go outside. , 
    Melissa likes to go outside too but she wouldn't have anybody to play with because 
    johnny's mom turned psycho. Kids get sunburned...that's just some kid stuff to do... 
    why are you upset at that?    
]   
Boris
  • 566
  • 5
  • 21
0

You can use a Scanner:

...
List<String> list = new LinkedList<>();                                         
                                                                                
try {Scanner scanner =                                                          
        new Scanner(new File(fileName)).useDelimiter("(\n{2,}|\n?\\z)")) {      
    while (scanner.hasNext()) {                                                 
        list.add(scanner.next());                                               
    }                                                                           
}                                                                               
                                                                                
return list.toArray(new String[] { });
...
Allen D. Ball
  • 1,916
  • 1
  • 9
  • 16