2016-05-10 32 views
-1
public class Try{ 
public static void main (String args[]) throws IOException { 
    BufferedReader in = new BufferedReader(new FileReader("Try.txt")); 
    Scanner sc = new Scanner(System.in); 
    System.out.print("Enter the subtring to look for: "); 
    String Word=sc.next(); 
    String line=in.readLine(); 
    int count =0; 
    String s[]; 
    do 
    { 
     s=line.split(" "); 
     for(int i=0; i < s.length; i++) 
     { 
      String a = s[i]; 
      if(a.contains(Word)) 
       count++; 
     } 
     line=in.readLine(); 
    }while(line!=null); 
    System.out.print("There are " +count+ " occurences of " +Word+ " in "); 
    java.io.File file = new java.io.File("Try.txt"); 
    Scanner input = new Scanner(file); 
    while(input.hasNext()) 
    { 
      String word = input.nextLine(); 
      System.out.print(word); 
    } 

    } 
} 

我的程序的預期用途是要求用戶輸入將在文本文件中檢查的特定單詞並且是否存在它會計算用戶輸入的單詞在文本文件中出現的次數。到目前爲止,我的程序只能搜索一個單詞。如果我嘗試用空格分隔兩個單詞,則只會搜索第一個單詞並計算其出現次數。有關如何搜索多個單詞的任何提示?詢問單詞並檢查其在文本文件中的出現次數

+0

程序中的什麼邏輯會讓你覺得它會搜索多個單詞?你只有一個'if(a.contains(Word))'行和一個計數器。您需要爲每個輸入的單詞保留計數器。 – KevinO

+0

'String Word = sc.next();'這意味着你正在讀一個名爲'Word'的字符串。即使你要改變這條線,你也需要做更多的事情來確保你要找的兩個詞不相鄰。您也可以運行調試器來檢查「Word」的內容,看看它是否只包含一個值。請參閱[Scanner.next()](https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#next()) –

回答

1

我下面硬是問題的標題,所以我會建議這個算法:

public static void main(String[] args) throws IOException { 
    BufferedReader in = new BufferedReader(new FileReader("Test.txt")); 
    Scanner sc = new Scanner(System.in); 
    System.out.print("Enter the subtring to look for: "); 
    String word = sc.next(); 
    String line = in.readLine(); 
    int count = 0; 
    // here is where the efficiently magic happens 
    do { 
    // 1. you dont need to split a line by spaces, too much overhead... 
    // 2. and you dont need to do counter++ 
    // 3. do instead: calculate the number of coincidences that the word is 
    //repeated in a whole line...that is what the line below does.. 
     count += (line.length() - line.replace(word, "").length())/word.length(); 
    //the rest looks fine 

    //NOTE: if you need a whole word then wrap the input of the user and add the empty spaces at begin and at the end...so the match will be perfect to a word 

     line = in.readLine(); 
    } while (line != null); 
    System.out.print("There are " + count + " occurences of " + word + " in "); 
    } 

編輯:

,如果你想在文檔中檢查多個單詞,然後使用這個

public static void main(String[] args) throws IOException { 
    BufferedReader in = new BufferedReader(new FileReader("Test.txt")); 
    Scanner sc = new Scanner(System.in); 
    System.out.print("Enter the subtring to look for: "); 
    String input = sc.nextLine(); 
    String[] word = input.split(" "); 
    String line = in.readLine(); 
    int count = 0; 
    do { 
     for (String string : word) { 
      count += (line.length() - line.replace(string, "").length())/string.length(); 
     } 

     line = in.readLine(); 
    } while (line != null); 
    System.out.print("There are " + count + " occurences of " + Arrays.toString(word) + " in "); 
} 
+0

這很短,但似乎問題依然存在。我嘗試輸入兩個單詞,但只記錄了第一個單詞的發生(使用您的算法)。任何方式來「修復」?順便謝謝你的建議:)肯定會注意到這些。 – REA98

+1

我會修復它...你是否在同一行中輸入空格分隔符? –

+0

是的,用空格隔開! – REA98

相關問題