Hello to everyone!

I am working on a simple application like a search engine.It searches in an index(a txt file with filenames). If a match is found, it displays the complete path where the keyword is contained and opens the path. It's a movie catalog and searches for the movie you want and opens also the dir .

My problem is this: i have made the txt file with the paths which will be used as the index. I use the FileReader for loading the txt.For example.C:\index.txt and contains the following string lines:
D:\2011 MOVIES\Movie1
D:\2011 MOVIES\Movie2
D:\2011 MOVIES\Movie3
D:\2011 MOVIES\Movie4
The app should search only for the Movie1,2,3,4 and ignore the rest of the string.
I know that i should probably do it with string split but none of the examples i found is right. I think that there's a solution with +\W character but i am not sure.

Does anyone know how to achive this silly string seperation?


Really thank you for your time :)

Armani_2 commented: Gonna try this bruh +0

Please show the String you have as input and what you want the output to be.
The methods split or indexOf and substring could be useful for extracting parts of a String.

Post the code that shows how you are trying to solve the String parsing problem.

Hello to everyone!

I am working on a simple application like a search engine.It searches in an index(a txt file with filenames). If a match is found, it displays the complete path where the keyword is contained and opens the path. It's a movie catalog and searches for the movie you want and opens also the dir .

My problem is this: i have made the txt file with the paths which will be used as the index. I use the FileReader for loading the txt.For example.C:\index.txt and contains the following string lines:
D:\2011 MOVIES\Movie1
D:\2011 MOVIES\Movie2
D:\2011 MOVIES\Movie3
D:\2011 MOVIES\Movie4
The app should search only for the Movie1,2,3,4 and ignore the rest of the string.
I know that i should probably do it with string split but none of the examples i found is right. I think that there's a solution with +\W character but i am not sure.

Does anyone know how to achive this silly string seperation?


Really thank you for your time :)

hmm this is a confusing question, do you mean that it must search for all files that have the names Movie1, Movie2 etc regardless if the entire name is D:\2011 MOVIES\Movie1_whatever_wahtever or D:\2011 MOVIES\Movie2_whatever

hmm this is a confusing question, do you mean that it must search for all files that have the names Movie1, Movie2 etc regardless if the entire name is D:\2011 MOVIES\Movie1_whatever_wahtever or D:\2011 MOVIES\Movie2_whatever

To make it clearer. The input is a txt file which is read via the FileReader and saved in a string variable.
The txt contains these:
D:\MOVIES\2011\SUN IS SHINING.avi
D:\MOVIES\2011\TWO PEOPLE.avi
D:\MOVIES\2011\TWO.avi
D:\MOVIES\2011\HELP.avi
D:\MOVIES\2011\NOONE.avi
D:\MOVIES\2011\WHAT IS THIS.avi
D:\MOVIES\2011\TRALALA.avi
D:\MOVIES\2011\OLEOLE.avi
D:\MOVIES\2011\SUMERTIME IN PRAGUE.avi
D:\MOVIES\2011\DOGTOOTH.avi
D:\MOVIES\2011\EYEYTOOTH.avi
So, when i type sth in a textfiled, it has to search only after the last slash ( ignore D:\MOVIES\2011\ and search the filenames.avi)
Notice that i have a movie called "TWO PEOPLE" and one called"TWO". when i type two, it should display both of them :P
In fact i think it is simple, it is like a simple search in a txt index.I But i can't find a template! My God! :)

This is how you create the index: you don't need to read in the files via a reader; you have to create an index.

Create a Map for you index and a File object like so

Map<String, String> fileIndex = new HashMap<String, String>();
File movies = new File("D:/MOVIES");

// when you find a file index its value like so
fileIndex.put(file.getName(), file.getAbsolutePath());

perform a recursive loop through the movies object using the listFiles method and every time you find a file use the files name as the fileIndex key and get the absolute path of the file and that should be the fileIndex value.

Now you have your index build; to search just search for the key in the fileIndex then get its value.

Simples :)

To make it clearer. The input is a txt file which is read via the FileReader and saved in a string variable.
The txt contains these:
D:\MOVIES\2011\SUN IS SHINING.avi
D:\MOVIES\2011\TWO PEOPLE.avi
D:\MOVIES\2011\TWO.avi
D:\MOVIES\2011\HELP.avi
D:\MOVIES\2011\NOONE.avi
D:\MOVIES\2011\WHAT IS THIS.avi
D:\MOVIES\2011\TRALALA.avi
D:\MOVIES\2011\OLEOLE.avi
D:\MOVIES\2011\SUMERTIME IN PRAGUE.avi
D:\MOVIES\2011\DOGTOOTH.avi
D:\MOVIES\2011\EYEYTOOTH.avi
So, when i type sth in a textfiled, it has to search only after the last slash ( ignore D:\MOVIES\2011\ and search the filenames.avi)
Notice that i have a movie called "TWO PEOPLE" and one called"TWO". when i type two, it should display both of them :P
In fact i think it is simple, it is like a simple search in a txt index.I But i can't find a template! My God! :)

well what about the contains() method which for example if you said:

if(path.toLowerCase.contains("two")) {//path refers to the full path of the file 
}
commented: njip +15

@javitis sorry didn't read your post properly the code I posted earlier is for building an index i.e generating an index file which you already have. to read an index from the file you already have use this code

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;

public class MovieIndexReader {
	private final Map<String, String> moveIndex = new HashMap<String, String>();
	private File indexPath;
	private final String DELIMITER = "\\\\";
	
	public MovieIndexReader(File file){
		indexPath = file;
	}
	
	public MovieIndexReader(String indexPath){
		this(new File(indexPath));
	}
	
	public void load(){
		Scanner scanner = null;
		
		try {
			scanner = new Scanner(indexPath);
			while(scanner.hasNextLine()){
				String line = scanner.nextLine();
				extractMovie(line);
			}
		} catch (FileNotFoundException e) {
			throw new RuntimeException(e);
		}finally{
			scanner.close();
		}
	}
	
	public Map<String, String> getMovieIndex(){
		return moveIndex;
	}
	
	private void extractMovie(String line){
		String[] tokens = line.split(DELIMITER);
		String movieName = tokens[tokens.length - 1];
		movieName = movieName.substring(0, movieName.lastIndexOf("."));
		moveIndex.put(movieName, line);
		
	}
	
	// Test
	public static void main(String[] args){
		MovieIndexReader miReader = new MovieIndexReader("movie_index.txt");
		miReader.load();
		
		for(Entry<String, String> entry : miReader.getMovieIndex().entrySet()){
			System.out.printf("%s : %s\n", entry.getKey(), entry.getValue());
		}
	}
	
}

To see the search in action use the code below.

public static void main(String[] args){
		MovieIndexReader reader = new MovieIndexReader("movie_index.txt");
		reader.load();
		Map<String, String> movieIndex = reader.getMovieIndex();
		
		while (true) { // put your terminating condition
			String movie = JOptionPane.showInputDialog("Enter movie title ");
			if (movie != null) {
				String msg = movie + " not found in system";
				List<String> results = find(movie, movieIndex);

				if (results.size() > 0) {
					msg = "found " + results.size() + " movie containing the word " + movie
							+ ":\n";
					for (String path : results) {
						msg += "\n" + path;
					}
				}

				JOptionPane.showMessageDialog(null, msg);
			}
		}
	}
	
	public static List<String> find(String query, Map<String, String> index){
		List<String> result = new ArrayList<String>();
		for(String movie : index.keySet()){
			if(movie.startsWith(query.toUpperCase())){
				result.add(index.get(movie.toUpperCase()));
			}
		}
		return result;
	}

fill free to clean up the code how you want; you've got your logic so u can do it your own way

hmm, I first thought your problem was, when you had:

D:\MOVIES\2011\SUN IS SHINING.avi

to print only

SUN IS SHINING.avi

for having:
D:\MOVIES\2011\SUN IS SHINING.avi
D:\MOVIES\2011\SUN IS SHINING 2.avi

if you want them both, contains("SUN") would be what you want
if you need to print out the title alone, not the whole path,
the lastIndexOf and substring method of String can do this for you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.