In this project you will be designing and implementing a min

In this project, you will be designing and implementing a mini search engine. You are probably familiar with Google or Yahoo, which are some of the most popular search engines that you can find on the Web. The task performed by a search engine is, as the name says, to search through a collection of documents. Given a set of texts and a query, the search engine will locate all documents that contain the keywords in the query. The problem may be therefore reduced to a search problem, which can be efficiently solved with the data structures we have studied in this class. Here is a simple example that opens a file, reads a text file and store the information in an array data structure. importjava.io.File; importjava.util.Scanner; importjava.io.FileNotFoundException; //This class demonstrates reading a text file and storing them into an array public class ReadFileIntoArray { public static void main(String[] args) { inti = 0; try { String[] words = new String[5000]; //Assuming the input file has max size of 5000 words Scanner input = new Scanner(System.in); //Scanner object for keyboard input System.out.print( \"Enter the filename: \" ); // Prompt the user for a file name String fileName = input.nextLine(); // get a file name from the user //If the full path is not given, then the file must be on the same folder as the source java file. //Otherwise, the user must give the full path File inputFile = new File(fileName ); // create a File object //Another Scanner object for file input Scanner fileInput = new Scanner(inputFile); while (fileInput.hasNext()) //Read from file as long as you have strings { words[i] = fileInput.next(); //Read a single string and store it in the array i++; } //Verify that you have the words stored in the array for (i = 0; i

Solution

ANSWER:

import java.io.File;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Scanner;
import java.io.FileNotFoundException;

public class UsingHashTable {

   @SuppressWarnings(\"resource\")
   public static void main(String[] args) {
       Hashtable<String, String> hashTable = new Hashtable<String, String>();
       try {
           Scanner input = new Scanner(System.in);
           char option;
           System.out.print(\"Enter the filename you want to search text: \");

           String fileName = input.nextLine();

           File inputFile = new File(fileName);

           Scanner fileInput = new Scanner(inputFile);
           while (fileInput.hasNext()) {
               String value = fileInput.next();
               hashTable.put(value, new String(value));
           }

           do {
               System.out.print(\"Enter the text you want to search: \");
               String searchText = input.next();
               boolean flag = false;
               Enumeration<String> names;
               names = hashTable.keys();
               while (names.hasMoreElements()) {
                   String str = (String) names.nextElement();
                   // System.out.println(str + \": \" + hashTable.get(str));
                   if ((hashTable.get(str).toLowerCase()).equals(searchText
                           .toLowerCase())) {
                       flag = true;
                   }
               }
               if (flag) {
                   System.out.println(\"\'\" + searchText
                           + \"\' Text find in the file.\");
               } else
                   System.out.println(\"\'\" + searchText
                           + \"\' Text not find in the file. Please try again\");
               System.out.println(\"Do you want to continue:(y/n)\");
               option = input.next().charAt(0);
           } while (option == \'y\' || option == \'Y\');

       } catch (FileNotFoundException e) {
           System.out.println(\"Error: \" + e.getMessage());
       }

   }

}

Explantion:

1) Here I\'m implemeted same code using HashTable()

2) It elements the duplicates while adding the data into HashTable() using put() method

________________________________________________________________________________________________

import java.io.File;
import java.util.Scanner;
import java.io.FileNotFoundException;

public class ReadFileIntoArray {
   @SuppressWarnings(\"resource\")
   public static void main(String[] args) {
       int i = 0;

       try {
           String[] words = new String[5000];
           Scanner input = new Scanner(System.in);
           char option;
           System.out.print(\"Enter the filename you want to search text: \");

           String fileName = input.nextLine();

           File inputFile = new File(fileName);

           Scanner fileInput = new Scanner(inputFile);
           while (fileInput.hasNext()) {
               words[i] = fileInput.next();
               i++;
           }

           do {
               System.out.print(\"Enter the text you want to search: \");
               String searchText = input.next();
               boolean flag = false;
               for (int j = 0; j < i; j++) {
                   if (words[j].toLowerCase().equals(searchText.toLowerCase())) {
                       flag = true;
                       break;
                   }
               }
               if (flag) {
                   System.out.println(\"\'\" + searchText
                           + \"\' Text find in the file.\");
               } else
                   System.out.println(\"\'\" + searchText
                           + \"\' Text not find in the file. Please try again\");
               System.out.println(\"Do you want to continue:(y/n)\");
               option = input.next().charAt(0);
           } while (option == \'y\' || option == \'Y\');

       } catch (FileNotFoundException e) {
           System.out.println(\"Error: \" + e.getMessage());
       }

   }

}

Explanation:

1) Read the file name from the user and store in data in string array

2) After that give a input you want to search in word from the file

3) If you want to continue give input (Y or y) else (N or n)

4) Again search word from the file

In this project, you will be designing and implementing a mini search engine. You are probably familiar with Google or Yahoo, which are some of the most popular
In this project, you will be designing and implementing a mini search engine. You are probably familiar with Google or Yahoo, which are some of the most popular
In this project, you will be designing and implementing a mini search engine. You are probably familiar with Google or Yahoo, which are some of the most popular

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site