【发布时间】:2013-09-26 17:58:34
【问题描述】:
我正在尝试使用扫描仪读取使用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 上更新它吗?
-
让它打印到控制台。我想可能是因为这个词后面跟着一个句号。
标签: java