search engine using java example

Solutions on MaxInterview for search engine using java example by the best coders in the world

showing results for - "search engine using java example"
Caterina
19 Aug 2019
1import java.util.*;
2import java.io.*;
3
4public class SearchEngine{
5	public static void main(String[] args){
6		Hashtable<String, ArrayList<String> > ht = new Hashtable<String, ArrayList<String> >();
7		Scanner kb = new Scanner(System.in);
8		System.out.println("Enter the filename that you want to Search values for.");
9		BufferedReader br = null;
10		try{
11			br = new BufferedReader(new FileReader(kb.nextLine()));//reads information from the file specified by user input
12			System.out.println("The file was read. Processing information, please wait...");
13
14			while(br.ready()){//should repeat until there are no more lines to read
15				String line = br.readLine();//assigns the line read by the reader to line
16				String[] result = line.split("\\s");//tokenizes the line into seperate strings, based on spaces only
17
18				for(int i = 0; i < result.length; i++){
19					if(!ht.containsKey(result[i])){
20						ArrayList<String> temp = new ArrayList<String>(1);
21						temp.add(line);
22						ht.put(result[i], temp);//assigns a key to anonymous ArrayList that stores the value
23					}
24					else{
25						ArrayList<String> temp = (ArrayList<String>)ht.get(result[i]);//if the key has already been assigned, thats ok
26						temp.add(line);                                               //just add the argument to the ArrayList!
27					}
28				}
29			}
30		}
31		catch(Exception e){
32			System.out.println(e);
33			System.exit(1);
34		}
35		System.out.println(ht);
36		do{
37			System.out.println("Enter a key to search for the value it is associated with.\n");
38			System.out.println(ht.get(kb.nextLine()));
39			System.out.println("\nKeep searching? Enter any key to continue, or type <NO> to end the process");
40		}while(!kb.nextLine().equalsIgnoreCase("<NO>"));
41		try{
42			br.close();
43		}
44		catch(Exception e){
45			System.out.println(e);
46			System.exit(1);
47		}
48	}//end main
49}//end class