【问题标题】:Multiple Searches in a single text File在单个文本文件中进行多次搜索
【发布时间】:2013-01-09 14:01:57
【问题描述】:

到目前为止,我已经制作了一个可以在文本文件中搜索单个字符串的程序。它确实给出了我搜索的字符串的索引。但是,如何在单个文本文件中搜索多个字符串?

到目前为止,如果我使用扫描仪输入“Alice”,我的输出将是:

Got a match at line 17

Got a match at line 19

Got a match at line 20

Got a match at line 21

但是,如果可能的话,我如何在我的扫描仪中搜索 Alice、John、Steven 作为我的输入和我的输出如下:

Got a match for Alice at line 17

Got a match at John at line 19

Got a match at Steven at line 20

Got a match at Steven at line 21

.

public static void main(String[]args) throws IOException{

    int nnames;
    String[] names;
    String stringSearch = null;

    Scanner scan = new Scanner(System.in);
    //for(int i=1;i<=3;i++){
    stringSearch = scan.nextLine();

    ArrayList<String> words = new ArrayList<String>();
    BufferedReader reader = new BufferedReader(new FileReader("File1.txt"));


    String line;

    while ((line = reader.readLine()) != null) {                
        words.add(line);
    }reader.close();


    Collections.sort(words);
     System.out.println("ArrayList elements after sorting in ascending order : ");
     System.out.println("");
        for(int i=0; i<words.size(); i++)
          System.out.println(words.get(i));


    for(String sLine : words) 
    {
        if (sLine.contains(stringSearch)) 
        {
            int index = words.indexOf(sLine);
            System.out.println("");
            System.out.println("Got a match at line " + index);

        }
     }



    System.out.println(words.size());
    }

【问题讨论】:

  • 定义“在单个文件中搜索多个字符串”。 2(或更多)字符串搜索的输出应该是什么?您是否正在寻找与这些字符串匹配的索引?请详细说明。
  • @amit 我已经更新了问题...希望您能更好地理解它?谢谢

标签: java arrays algorithm sorting binary-search


【解决方案1】:

用你正在寻找的所有字符串的循环替换你的if-block

for(String sLine : words) 
{
    for(String searchWord: searchWords)
    {
        if (sLine.contains(searchWord)) 
        {
            int index = words.indexOf(sLine);
            System.out.println("");
            System.out.println("Got a match of "+searchWord+" at line " + index);
        }
    }
 }

【讨论】:

  • 您可以避免indexOf 使用 simple for,而不是 foreach 来获取单词的当前索引。
  • @MrSmith42 我对 for(String searchWord: searchWords) 的那一行感到困惑,这是否意味着我现在必须声明一个名为“searchWords”的新变量?
  • 将您搜索的所有单词存储在包含字符串的String[] searchWord 或其他Iterable 中。如果 args 数组包含您搜索的所有单词,您也可以简单地使用它。
【解决方案2】:

试试这个

import java.util.Scanner;
import java.util.ArrayList;
import java.util.*;
import java.io.*;
class x{
public static void main(String[]args) throws IOException{

    int nnames;
    String[] names;
    String stringSearch[] = new String[2];

 //   Scanner scan = new Scanner(System.in);
    //for(int i=1;i<=3;i++){
 //   stringSearch = scan.nextLine();

    stringSearch[0]="hello";   //list of words to search
    stringSearch[1]="world";
    ArrayList<String> words = new ArrayList<String>();
    BufferedReader reader = new BufferedReader(new FileReader("File1.txt"));


    String line;

    while ((line = reader.readLine()) != null) {                
        words.add(line);
    }reader.close();


    Collections.sort(words);
//     System.out.println("ArrayList elements after sorting in ascending order : ");
//     System.out.println("");
        // for(int i=0; i<words.size(); i++)
          // System.out.println(words.get(i));

for(int i=0;i<2;i++){
    for(String sLine : words) 
    {
        if (sLine.contains(stringSearch[i])) 
        {
            int index = words.indexOf(sLine);
            System.out.println("");
            System.out.println("Got a match of "+stringSearch[i] +" at line " + index);

        }
     }}



    System.out.println(words.size());
    }
    }

如果你想从输入中获取所有搜索字符串 然后试试这个

searchString=item to search seperated by ','
String[] splits = searchString.split(",");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-01
    • 1970-01-01
    • 2013-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多