2

I have a data like this

1|Toy Story (1995)|01-Jan-1995||http://us.imdb.com/M/title-exact?Toy%20Story%20(1995)|0|0|0|1|1|1|0|0|0|0|0|0|0|0|0|0|0|0|0

2|GoldenEye (1995)|01-Jan-1995||http://us.imdb.com/M/title-exact?GoldenEye%20(1995)|0|1|1|0|0|0|0|0|0|0|0|0|0|0|0|0|1|0|0

3|Four Rooms (1995)|01-Jan-1995||http://us.imdb.com/M/title-exact?Four%20Rooms%20(1995)|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|1|0|0

4|Get Shorty (1995)|01-Jan-1995||http://us.imdb.com/M/title-exact?Get%20Shorty%20(1995)|0|1|0|0|0|1|0|0|1|0|0|0|0|0|0|0|0|0|0

5|Copycat (1995)|01-Jan-1995||http://us.imdb.com/M/title-exact?Copycat%20(1995)|0|0|0|0|0|0|1|0|1|0|0|0|0|0|0|0|1|0|0

and suppose the link part is in the same line with the movie names part.I am

only interested in movie numbers in the leftmost part and the movie names.

How can I read this file in Java and return like:

1|Toy Story

2|GoldenEye

Thanks for helping in advance.

ninesalt
  • 4,054
  • 5
  • 35
  • 75
  • 1
    Possible duplicate of [How to split a string in Java](https://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java) – Bernhard Barker Dec 30 '17 at 11:32

6 Answers6

1

Pretty easy, just split on " (" and remember to escape it using \\.

public static void main(String[] args) {

        String result = movie("1|Toy Story (1995)|01-Jan-1995||http://us.imdb.com/M/title-exact?Toy%20Story%20(1995)|0|0|0|1|1|1|0|0|0|0|0|0|0|0|0|0|0|0|0");
        System.out.println(result);   //prints 1|Toy Story
    }

    public static String movie(String movieString){
        return movieString.split(" \\(")[0];
    }
ninesalt
  • 4,054
  • 5
  • 35
  • 75
1

You can use regular expressions to extract the part that you want. It is assumed that a movie title only contains word characters or spaces.

List<String> movieInfos = Arrays.asList(
        "1|Toy Story (1995)|01-Jan-1995||http://us.imdb.com/M/title-exact?Toy%20Story%20(1995)|0|0|0|1|1|1|0|0|0|0|0|0|0|0|0|0|0|0|0",
        "2|GoldenEye (1995)|01-Jan-1995||http://us.imdb.com/M/title-exact?GoldenEye%20(1995)|0|1|1|0|0|0|0|0|0|0|0|0|0|0|0|0|1|0|0",
        "3|Four Rooms (1995)|01-Jan-1995||http://us.imdb.com/M/title-exact?Four%20Rooms%20(1995)|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|1|0|0",
        "4|Get Shorty (1995)|01-Jan-1995||http://us.imdb.com/M/title-exact?Get%20Shorty%20(1995)|0|1|0|0|0|1|0|0|1|0|0|0|0|0|0|0|0|0|0",
        "5|Copycat (1995)|01-Jan-1995||http://us.imdb.com/M/title-exact?Copycat%20(1995)|0|0|0|0|0|0|1|0|1|0|0|0|0|0|0|0|1|0|0"
);

Pattern pattern = Pattern.compile("^(\\d+)\\|([\\w\\s]+) \\(\\d{4}\\).*$");

for (String movieInfo : movieInfos) {

    Matcher matcher = pattern.matcher(movieInfo);
    if (matcher.matches()) {
        String id = matcher.group(1);
        String title = matcher.group(2);

        System.out.println(String.format("%s|%s", id, title));
    } else {
        System.out.println("Unexpected data");
    }

}
Ward
  • 2,799
  • 20
  • 26
0

This works only if you have all the lines formated like that.

private static final String FILENAME = "pathToFile";

public static void main(String[] args) {

    BufferedReader br = null;
    FileReader fr = null;
    ArrayList<String> output = new ArrayList<>();

    try {

        //br = new BufferedReader(new FileReader(FILENAME));
        fr = new FileReader(FILENAME);
        br = new BufferedReader(fr);

        String currentLine;

        while ((currentLine= br.readLine()) != null) {
            String movie = currentLine.split(" \\(")[0];
            output.add(movie);
        }

    } catch (IOException e) {

        e.printStackTrace();

    } finally {

        try {

            if (br != null)
                br.close();

            if (fr != null)
                fr.close();

        } catch (IOException ex) {

            ex.printStackTrace();

        }

    }

}
M. Alexandru
  • 614
  • 5
  • 20
0

Assuming you are reading t.txt

File file = new File("t.txt");    
        try {
            Scanner in = new Scanner(file);
            while(in.hasNextLine())
            {

                String arr[] = in.nextLine().split("\\|");
                if(arr.length > 1)
                {
                    System.out.println(arr[0] +"|"+arr[1].split("\\(")[0]);
                    System.out.println();
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

Will give you as an output

1|Toy Story 

2|GoldenEye 

3|Four Rooms

4|Get Shorty 

5|Copycat

There are 2 things which you have to take care in this. (Here we assume we are reading the first line)

  1. Split by |. Now since | is a meta character you have to use to escape it. Hence in.nextLine().split("\\|");

  2. Now arr[0] will contain 1 and arr[2] will contain Toy Story (1995). So we split arr[2] via "(". you need the first match hence you can write it as arr[1].split("\\(")[0]) (you again have to escape it as "(" is also a metacharacter).

PS : if(arr.length > 1) this line is there to avoid blank new lines so that you don't end up with ArrayIndexOutOfBoundsException.

Vikram Singh
  • 113
  • 9
0

Considering the file format is the same as you have given, read the file line by line and for each read line, split it on the "(" parenthesis and print the first index in the resultant array obtained after the split operation.

static void readMovieNamesFromFile(String fileName)  {
    try (BufferedReader br = new BufferedReader(new FileReader(new File(fileName)))) {
        String line;
        while( (line = br.readLine()) != null){
            System.out.println((line.split("\\(")[0]).trim());
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
-1

You can save data in String

For example

String name =  //data of move

Then use if with is char

for(int i =0;i<name.lenght;i++)
   {
       if(name.charat(i).equals("(")  //will read when it catch ( after name it will stop 
            {Break;}
            Else 
               System.out.print("name.charat(i);
 }

You can also fixt by other way

user326260
  • 45
  • 5