I\'m working on a database assignment in Java and struggling with separating my
ID: 3842325 • Letter: I
Question
I'm working on a database assignment in Java and struggling with separating my data.
I have been given several CSV files, one of which is a list of movies. They are separated as listed below:
1::Toy Story (1995):: Animated | Family
My job is to extract the data from each and upload to an SQL server where I will then run queries. However, before I upload it, I need to separate my data. For example, my professor wants the year and movie in a separate column, and we need to avoid repeating groups for genres (meaning I need to extract the genre from my initial movies CSV and create a separate table with a movie/genre relationiship).
I've already created an array which stores all the data from the Movies CSV, however I'm struggling with getting the (1995) into a separate array as well as the genres. I've read a lot about regex and pattern matching but I can't seem to get anything to work. Here's what I've been attempting:
for(int i = 1; i < movies.length;){
years = movies[i].split("<regex formatting here>");
i = i + 3;
}
}
I increment i by 3 as the element with the movie title comes on every 3rd after element 1. Any tips are greatly appreciated!
Explanation / Answer
Here is a piece of code that explains how you can split the string to get year and the generes separately. For year i did not use regex because it was simple through indexOf() method. But for generes I used regex.
PROGRAM CODE:
import java.util.*;
import java.lang.*;
import java.io.*;
public class SplitString
{
public static void splitting(String str)
{
//we know that year ends with ')::'. So we can use this to get the year value
int indexOfYearEnding = str.indexOf(")::");
// we just got the last index. So the previous 4 indexes will give the year
int year = Integer.valueOf(str.substring(indexOfYearEnding-4, indexOfYearEnding));
System.out.println(year);
// this is for getting the generes into array
// '\|' is the regex you can use to get the array of generes. '|' has a separate meaning in regex
// hence we have to escape it inorder to split the string based on '|'' as the delimiter
String generes[] = str.substring(indexOfYearEnding+3).split("\|");
for(int i=0; i<generes.length; i++)
System.out.println(generes[i].trim()); //trim off the extra spaces
}
public static void main (String[] args) throws java.lang.Exception
{
splitting("1::Toy Story (1995):: Animated | Family");
}
}
OUTPUT:
1995
Animated
Family