rasm937k
rasm937k

Reputation: 101

Read and split multiple lines into individual strings

I hope you are well. Currently I'm struggling with a school project in Java. We have to create "a streaming/entertainment service" where a host is able to "host a show", with this not being actually implemented. I've made it so that the host of the "show" can fill in all needed informations about the show and writes this to a file. In order to show the "viewers" what shows is about to come, I need to read this from the same file and assemble all the lines as one String. My data set is currently as

Title
Genre
Entertainment type
Date and time
Length of show
Price of show (if any)
"-"

I've made the "-" in the end of each "show" to be able to split on this, but I can't seem to have any luck with this. A snippet of a data set would be:

Show #1
Stream
Review
2020-10-10T10:00
90
0.0
-
Show#2
Stream
Review
2020-10-10T10:00
90
0.0

What I thought to do:

public class ShowHandler{
private String[] shows;
public void readShows(){
BufferedReader bufferedReader = new BufferedReader(new FileReader("Shows.txt"));
String line;
while((line = bufferedReader.readLine()) != null){
//split the read lines when the "-" occurs
//add these to each place in an array/list
    }
  }
}

The two comments in the while loop is what I thought would do the trick, but somehow I haven't been able to do this. Any clever and experienced minds who can help me?

Best regards.

Upvotes: 0

Views: 147

Answers (1)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79620

split the read lines when the "-" occurs

Do not split lines on - as each record also has a date field which has -. You can use an instance of List<List<String>> to store each record where each record can be an instance of List<String>.

Read each line and put into a List<String> and as soon as a - occurs, copy it to the instance of List<List<String>> and reset List<String> to gather the lines for the next record.

Demo:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) throws IOException {
        // Test
        readShows().forEach(System.out::println);
    }

    static List<List<String>> readShows() throws IOException {
        List<List<String>> listOfLists = new ArrayList<>();
        BufferedReader bufferedReader = new BufferedReader(new FileReader("Shows.txt"));
        String line;
        List<String> list = new ArrayList<>();
        while ((line = bufferedReader.readLine()) != null) {
            // Transfer the data from `list` to `listOfLists` and create a new list as soon
            // as `-` occurs
            if (line.equals("-")) {
                listOfLists.add(list);
                list = new ArrayList<>();
            } else {
                list.add(line);
            }
        }
        // Add the last record
        listOfLists.add(list);

        bufferedReader.close();
        return listOfLists;
    }
}

Output:

[Show #1, Stream, Review, 2020-10-10T10:00, 90, 0.0]
[Show#2, Stream, Review, 2020-10-10T10:00, 90, 0.0]

Upvotes: 1

Related Questions