Assigning the output as a new String

Viewed 49

I am trying to assign the output of this code as a new string variable, currently, the output is printed from a for loop, I am using split/tokens to change each "," to a ">". The output I want is correct, but I would like to use a delimiter ">" afterwards to separate each line into separate categories. Here is my code...

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class tests {

    public static void main(String[] args) throws IOException {
        String fileContent = "";
        String number = "";
        try {
        Scanner file = new Scanner(new File("netflix_titles.txt")); 
        while(file.hasNextLine()){
            fileContent = fileContent.concat(file.nextLine() + "\n");
        }
        
            String[] tokens = fileContent.split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)", -1);
            for(String t : tokens) {
                System.out.print("> " + t);
            }
        }
        catch(Exception e){
            System.out.println("invalid input");
        }   
    }
}

The first line of the file is

s1,TV Show,3%,,"João Miguel, Bianca Comparato, Michel Gomes, Rodolfo Valente, Vaneza Oliveira, Rafael Lozano, Viviane Porto, Mel Fronckowiak, Sergio Mamberti, Zezé Motta, Celso Frateschi",Brazil,"August 14, 2020",2020,TV-MA,4 Seasons,"International TV Shows, TV Dramas, TV Sci-Fi & Fantasy","In a future where the elite inhabit an island paradise far from the crowded slums, you get one chance to join the 3% saved from squalor."

After the code is compiled it looks like

s1> TV Show> 3%> > "João Miguel, Bianca Comparato, Michel Gomes, Rodolfo Valente, Vaneza Oliveira, Rafael Lozano, Viviane Porto, Mel Fronckowiak, Sergio Mamberti, Zezé Motta, Celso Frateschi"> Brazil> "August 14, 2020"> 2020> TV-MA> 4 Seasons> "International TV Shows, TV Dramas, TV Sci-Fi & Fantasy"> "In a future where the elite inhabit an island paradise far from the crowded slums, you get one chance to join the 3% saved from squalor."

I would like to be able to use a ">" delimiter afterwards to create an object for each line with individual constructors split by the >. Since it is only being outputted through a for loop, I am not sure how to use a delimiter after its been outputted. Sorry if this is confusing I am not sure how to correctly word this.

1 Answers

One approach would be to use the following regex pattern with a Java pattern matcher:

(\".*?\"|[^,]*),?

This regex pattern will attempt to first match a double quoted term which might also have commas inside it. That failing, it will fall back to matching any CSV term up to a comma, capturing only the content, but not the comma. Here is a short script using your sample input:

String input = "s1,TV Show,3%,,\"João Miguel, Bianca Comparato, Michel Gomes, Rodolfo Valente, Vaneza Oliveira, Rafael Lozano, Viviane Porto, Mel Fronckowiak, Sergio Mamberti, Zezé Motta, Celso Frateschi\",Brazil,\"August 14, 2020\",2020,TV-MA,4 Seasons,\"International TV Shows, TV Dramas, TV Sci-Fi & Fantasy\",\"In a future where the elite inhabit an island paradise far from the crowded slums, you get one chance to join the 3% saved from squalor.\"";

Pattern p = Pattern.compile("(\".*?\"|[^,]*),?");
Matcher m = p.matcher(input);
StringBuffer buffer = new StringBuffer();

while(m.find()) {
    String replacement = "";
    if (buffer.length() > 0) replacement = "> ";
    if (m.group(1).length() > 0) m.appendReplacement(buffer, replacement + m.group(1));
}
m.appendTail(buffer);
System.out.println(buffer.toString());

This prints:

s1> TV Show> 3%> > "João Miguel, Bianca Comparato, Michel Gomes, Rodolfo Valente, Vaneza Oliveira, Rafael Lozano, Viviane Porto, Mel Fronckowiak, Sergio Mamberti, Zezé Motta, Celso Frateschi"> Brazil> "August 14, 2020"> 2020> TV-MA> 4 Seasons> "International TV Shows, TV Dramas, TV Sci-Fi & Fantasy"> "In a future where the elite inhabit an island paradise far from the crowded slums, you get one chance to join the 3% saved from squalor."

Related