我正在製作一個簡單的Java程序,它應該讀取一個txt文件並計算特定單詞被提及的次數。這是我在下面,它編譯和運行良好,但一直返回0的答案時,它應該運行良好的成千上萬...單詞被提及的次數
在這種情況下,我想知道多少次「我通過」此文件...
/*AUTHOR Yun Lee
Finding out the number of tweets per day*/
import java.io.*;
import javax.swing.*;
import java.util.Scanner;
class tweetnumber
{
public static void main(String args[])
{
try
{
FileInputStream fstream = new FileInputStream("tacobell.23jan2012.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
int counter1 = 0;
String s = "am via";
while ((strLine = br.readLine()) != null)
{
System.out.println (strLine);
if (strLine.contains(s))
{
counter1++;
}
}
in.close();
System.out.println("There were " + counter1 + " messages in the AM of this day");
}
catch (Exception e)
{
System.err.println("Error: " + e.getMessage());
}
}
}
該文件是否由您的'println'語句打印?試圖設置一個斷點並檢查'strLine'以確保它包含「am via」? – Blorgbeard 2012-02-13 17:50:17
這應該被標記爲家庭作業嗎?順便說一下,如果您要查找的字符串連續出現多次,會發生什麼情況? – jambriz 2012-02-13 17:56:46
「'」是通過「'應該只是一個單詞嗎?或者你是否在尋找「am」和「via」這兩個詞的出現? – 2012-02-13 18:36:30