我正在尝试使用扫描仪读取使用JFileChooser
拉出的文本文件。 wordCount
工作正常,所以我知道它正在阅读。但是,我无法得到它来搜索用户输入单词的实例。使扫描仪读取文本文件
public static void main(String[] args) throws FileNotFoundException {
String input = JOptionPane.showInputDialog("Enter a word");
JFileChooser fileChooser = new JFileChooser();
fileChooser.showOpenDialog(null);
File fileSelection = fileChooser.getSelectedFile();
int wordCount = 0;
int inputCount = 0;
Scanner s = new Scanner (fileSelection);
while (s.hasNext()) {
String word = s.next();
if (word.equals(input)) {
inputCount++;
}
wordCount++;
}
给我们一个你的文件内容和输入的例子。 –
你是如何显示inputCount的?你在一些GUI上更新它吗? –
将它打印到控制台。我想这可能是因为这个词后面跟着一段时间。 – user2792660