0

したがって、データで満たされたテキスト ファイルを読み取り、いくつかのキーワードを使用してユーザーにファイルを検索させることによって、非常に基本的な検索エンジンをシミュレートしようとしています。ユーザーに。これまでのコードはテキスト ファイルを読み取ることができますが (私の知る限り)、入力したキーワードに関係なく、ほとんどの場合、コンソールには結果が見つからないと表示されます (テキスト ファイルにキーワードが含まれていません)。ありがとうございます。みんな。

適切なコードは次のとおりです。

package myquery;

import java.util.Scanner;
import java.util.*;
import java.io.*;

public class MyQuery{

public static void main(String[] args){

    Hashtable<String, ArrayList<String> > hash = new Hashtable<String, ArrayList<String> >();
    Scanner input = new Scanner(System.in);
    System.out.println("Here is where your .txt file(s) should be located for this program to work properly:");
    System.out.println(new java.io.File("").getAbsolutePath());
    System.out.println("\nEnter the filename that you want to Search values for. For example \"MyQuery.txt\"");
    BufferedReader reader = null;

    try{
        reader = new BufferedReader(new FileReader(input.nextLine()));
        System.out.println("The file was found :) Here are the contents:");

        while(reader.ready())
        {
            String currentline = reader.readLine();
            String[] result = currentline.split("\\s");
            for(int i = 0; i < result.length; i++)
            {
                if(!hash.containsKey(result[i]))
                {
                    ArrayList<String> temp = new ArrayList<String>(1);
                    temp.add(currentline);
                    hash.put(result[i], temp);
                }
                else
                {
                    ArrayList<String> temp = (ArrayList<String>)hash.get(result[i]);//
                    temp.add(currentline);
                }
            }
        }
    }

    catch(Exception e)
    {
        System.out.println("Your file was not found unfortunately. Please try again.");
        System.exit(1);
    }

    System.out.println(hash);
    do
    {
        System.out.println("Enter a key to search for the value it is associated with.\n");
        System.out.println(hash.get(input.nextLine()));
        System.out.println("\nWant to try again? If so, press return and then follow the prompt. Type \"-1\" to quit");
    }
    while(!input.nextLine().equals("-1"));
    try
    {
        reader.close();
    }
    catch(Exception e)
    {
        System.out.println(e);
        System.exit(1);
    }
}
}

テキスト ファイル内の 1 行の例:

www.Mets.com, "The New York Mets", baseball, team, NY

プログラムがどのように応答するかの例を次に示します。

Enter a key to search for the value it is associated with.

Mets
www.Mets.com The New York Mets

Want to try again? If so, press return and then follow the prompt. Type "-1" to quit

しかし、ここに私が代わりに得ているものがあります:

Enter a key to search for the value it is associated with:

Mets
null

Want to try again? If so, press return and then follow the prompt. Type "-1" to quit

でも。最初/最後の単語以外の単語に一致する単語を入力すると、機能します。

例えば

Enter a key to search for the value it is associated with:

New
[www.Mets.com, "The New York Mets", baseball, team, NY, www.nytimes.com, "The New York Times", news]

Want to try again? If so, press return and then follow the prompt. Type "-1" to quit
4

0 に答える 0