my code stock.com

Sudheer Arikatla

list the names of other movies watched by audience who watched Avataar
by Sudheer Arikatla

Snippet options

Download: Download snippet as list-the-names-of-other-movies-watched-by-audience-who-watched-avataar.java.
Copy snippet: For this you need a free my code stock.com account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!

// Assuming the fields of Audience name and movie name are not null and seperated by tab (delimiter)
// I have not checked if Audience name and movie name are null for better understanding of the code

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

public class Movies {

	public static void main(String[] args) {
		String filepath = "file.txt";                        
		String line = null;
		String moviename = "Avataar";                               
		List<String> names = new ArrayList<>();       // list to store Audience names who watched Avataar movie  
		List<String> movies = new ArrayList<>();      // list to store movies watched by Audience who are in names list
		FileReader fileReader = null;
		BufferedReader bufferedReader = null; 
		try {
			
			fileReader = new FileReader(filepath);
			bufferedReader = new BufferedReader(fileReader);
			while ((line = bufferedReader.readLine()) != null) {
				String array[] = line.split("\t");     
				if (array[1].equals(moviename)) {      // if the movie is watched by current person store his name in names list 
					if (!names.contains(array[0])) {
						names.add(array[0]);
					}
				}
			}
			fileReader = new FileReader(filepath);
			bufferedReader = new BufferedReader(fileReader);
			while ((line = bufferedReader.readLine()) != null) {
				String array[] = line.split("\t");
				if (names.contains(array[0])) {         // store the movies watched by the current person in movies list, if    
					if (!movies.contains(array[1])) {   // the person is in names list
						movies.add(array[1]);
					}
				}
			}
			movies.remove(moviename);                   // remove the movie specified in the input 
			for (String answers : movies) {
				System.out.println(answers);            
			}
			
		} catch (FileNotFoundException e) {
			System.out.println("The file specified doesnot exist");
		} catch (IOException e) {
			System.out.println("Error occured while opening the file");
		} finally {
			try {
				bufferedReader.close();
			} catch (IOException e) {
				System.out.println("Resource cannot be closed");
			}
		}
	}
}

Create a free my code stock.com account now.

my code stok.com is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

Find out more and register now

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.