你的問題是if
你發現一條線匹配你break;
循環。因此,您的計數器將永遠不會超過1.
相反,您應該從代碼中刪除break;
,然後while
循環將遍歷所有行。
編輯:
下面的代碼已經過測試和工作。值得注意的總點有:
答:主類是加載的第一個類,因此必須運行任何要在程序啓動時運行的任何內容。而且主類也必須是靜態的。
B.我已經改變了Scanner
你使用,而不是一個BufferedReader
和FileReader
它們是不同的類,也被設計來讀取文件的內容。
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;
}
如果事情你'break'然後''while'循環將被從中逃脫。 –
那麼,**這**肯定是一個問題:'公共靜態void main(String [] args){}' –
也糾正你的縮進,所以你(和我們)可以看到預期的流程。現在@HovercraftFullOfEels編輯了你的帖子後,你應該能夠在一次迭代後看到循環中斷(無條件) –