2013-01-09 33 views
1

到目前爲止,我已經制作了一個程序,可以搜索文本文件中的單個字符串。它確實給出了我搜索的字符串的索引。但是,如何在單個文本文件中搜索多個字符串?在單個文本中進行多次搜索文件

到目前爲止,如果我輸入「愛麗絲」使用掃描儀我的輸出是:

Got a match at line 17 

Got a match at line 19 

Got a match at line 20 

Got a match at line 21 

然而,如果可能的話,我怎麼能在我的掃描儀搜索,愛麗絲,約翰,史蒂芬爲我輸入和我的輸出是這樣的:

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()); 
    } 
+0

定義「在單個文件中搜索多個字符串」。 2(或更多)字符串搜索的輸出應該是什麼?你在尋找與這些字符串匹配的索引嗎?請詳細說明。 – amit

+0

@amit我已經更新了這個問題......我希望你能更好地理解它?謝謝 – user1883386

回答

2

在所有的字符串由環更換你if - 阻塞你正在尋找

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); 
     } 
    } 
} 
+1

您可以使用_simple for_來避免'indexOf',而不是_foreach_來獲取當前的單詞索引。 –

+0

@ MrSmith42我很困惑那條線 for(String searchWord:searchWords) 這是否意味着我現在必須聲明一個名爲'searchWords'的新變量? – user1883386

+1

將您搜索的所有單詞存儲在包含字符串的'String [] searchWord'或其他'Iterable'中。如果它包含您搜索的所有單詞,您也可以簡單地使用'args'數組。 – MrSmith42

1

,如果你想從輸入 所有搜索字符串試試這個

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(",");