2017-05-24 36 views
-2

我不得不製作一個程序,用於在文本文件中搜索特定的字符串,用戶使用掃描儀輸入字符串,並返回文本文件中所用字符串的次數。當試圖以無數方式解決問題時,我不知何故以程序運行結束,但它向我詢問了兩次輸入信息,並始終只用「1」返回。我工作過,所以它已成爲一種方法,但我不知道如何運行此方法,因爲它確實調用了用戶選擇的文本文件和字符串。這是香港專業教育學院想出了這麼遠:什麼是運行檢查文本文件中的字符串的程序的適當方式?

import java.util.*; 
import java.io.*; 
import javax.swing.*; 
import java.lang.*; 

public class WordCount 
{ 


    public static int countWord(File dataFile, String WordToSearch) throws FileNotFoundException 
{ 
     int count = 0; 
     dataFile = new File("text.txt"); 
     Scanner FileInput = new Scanner(dataFile); 
     while (FileInput.hasNextLine()) 
{ 
      Scanner s = new Scanner(System.in); 
      String search = s.next(); 
      if (search.equals(WordToSearch)) 
       count++; 

     } 
     return count; 
    } 
} 

這是文本文件,我試着去上 稱之爲「嗨你好再見你好你好再見你好你好再見你好你好再見你好你好再見你好你好再見的內容嗨,你好,再見「

請讓我知道,如果你發現我可能錯過了代碼中的任何錯誤,非常感謝你的所有幫助。

+4

如果事情你'break'然後''while'循環將被從中逃脫。 –

+5

那麼,**這**肯定是一個問題:'公共靜態void main(String [] args){}' –

+2

也糾正你的縮進,所以你(和我們)可以看到預期的流程。現在@HovercraftFullOfEels編輯了你的帖子後,你應該能夠在一次迭代後看到循環中斷(無條件) –

回答

-2

你的問題是if你發現一條線匹配你break;循環。因此,您的計數器將永遠不會超過1.

相反,您應該從代碼中刪除break;,然後while循環將遍歷所有行。

編輯:

下面的代碼已經過測試和工作。值得注意的總點有:

答:主類是加載的第一個類,因此必須運行任何要在程序啓動時運行的任何內容。而且主類也必須是靜態的。

B.我已經改變了Scanner你使用,而不是一個BufferedReaderFileReader它們是不同的類,也被設計來讀取文件的內容。

C.我的while只有當它到達文件末尾時纔會循環breaks;。如果您以前break那麼它會停止搜索找到所有的事件。

D.我不確切地知道你在找什麼。例如,如果您想查找與您的單詞相同的整行或包含您單詞的整行(如果您更具體地告訴我如何優化您的搜索,我可以更新代碼)

//當你打包嚴重降低你的程序的大小,如果你不導入整個Java包 //這裏,我已經縮小了IO *和* UTIL實際上是你需要

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 
import java.util.Scanner; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

public class ScannerInt { 

    public static void main(String[] args) { 
     // the main method is the first that is run. 
     // the main method MUST be static 
     // inside the main method put whatever you want to run in your program 

     scan(); // we run the scanner method which is created below 
    } 

    public static void scan() { 
      // first i create a scanner so that the java command prompt can take input 
      Scanner input = new Scanner(System.in); 

      System.out.println("Enter file path to file and file name: "); //prompt the reader to type the file directory and name 
      File file = new File(input.nextLine()); // determine what file they want based on that name 

      System.out.println("Enter the line you want to find: "); //prompt the reader to type the line they want to count 
      String line = input.nextLine(); // determine what line they want based on that string 

      // this statement tells them how many occurances there are 
      // this is done by the countWord() method (which when run returns a integer) 
      System.out.println("There are " + countWord(file, line) + " occurances."); 

      System.exit(0); // ends the program 
    } 

    // the countWord method takes a file name and word to search and returns an integer 
    public static int countWord(File dataFile, String WordToSearch) { 

     int count = 0; // start are counter 

     // NOTE: i prefer a BufferedReader to a Scanner you can change this if you want 

     // load the buffered reader 
     BufferedReader FileInput = null; 
     try { 
      // attempt to open the file which we have been told about 
      FileInput = new BufferedReader(new FileReader(dataFile)); 
     } catch (FileNotFoundException ex) { 
      // if the file does not exist a FileNotFoundException will occur 
      Logger.getLogger(ScannerInt.class.getName()).log(Level.SEVERE, null, ex); 
     } 

     // then try searching the file 
     try { 
      // this while loop runs forever until it is broken below 
      while (true) { 
       String search = FileInput.readLine(); // we read a line and store it 

       if (search == null) { // if the line is "null" that means the file has ended and their are no more lines so break 
        break; 
       } 

       // then we check if this line has the text 

       // NOTE: i do not know what exactly you want to do here but currently this checks if the entire line 
       // is exactly equal to the string. This means though that if their are other words on the line it will not count 

       // Alternatively you can use search.contains(WordToSearch) 
       // that will check if the "WordToSearch" is anywhere in the line. Unfortunately that will not record if there is more than one 

       // (There is one more option but it is more complex but i will only mention it if you find the other two do not work) 
       if (search.equals(WordToSearch)) { 
        count++; 
       } 
      } 
     } catch (IOException ex) { 
      // if the line it tries to read does not exist a IOException will occur 
      Logger.getLogger(ScannerInt.class.getName()).log(Level.SEVERE, null, ex); 
     } 

     try { 
      FileInput.close(); // close the file reader 
     } catch (IOException ex) { 
      // if the FileInput reader has broken and therefore can not close a IOException will occur 
      Logger.getLogger(ScannerInt.class.getName()).log(Level.SEVERE, null, ex); 
     } 

     return count; 
    } 
+1

考慮到'main'方法中沒有什麼,我懷疑它會工作。 –

+0

正確。我編輯過它。我相信用戶有更多的代碼,他沒有發佈。 – JFreeman

+0

我很困惑如何運行這種方法,我試過使用這個,但沒有效果 – user3663055

相關問題