我尝试计算用户“输入”文件中的行数、单词数、字符数。
在此节目结束后,请继续计数并再次提问。
如果文件不存在,则打印运行过程中统计过的所有数据。
代码:
public class KeepAskingApp {
private static int lines;
private static int words;
private static int chars;
public static void main(String[] args) {
boolean done = false;
//counters
int charsCount = 0, wordsCount = 0, linesCount = 0;
Scanner in = null;
Scanner scanner = null;
while (!done) {
try {
in = new Scanner(System.in);
System.out.print("Enter a (next) file name: ");
String input = in.nextLine();
scanner = new Scanner(new File(input));
while(scanner.hasNextLine()) {
lines += linesCount++;
Scanner lineScanner = new Scanner(scanner.nextLine());
lineScanner.useDelimiter(" ");
while(lineScanner.hasNext()) {
words += wordsCount++;
chars += charsCount += lineScanner.next().length();
}
System.out.printf("# of chars: %d\n# of words: %d\n# of lines: ",
charsCount, wordsCount, charsCount);
lineScanner.close();
}
scanner.close();
in.close();
} catch (FileNotFoundException e) {
System.out.printf("All lines: %d\nAll words: %d\nAll chars: %d\n",
lines, words, chars);
System.out.println("The end");
done = true;
}
}
}
}
但我不明白为什么它总是显示不带参数的输出:
All lines: 0
All words: 0
All chars: 0
The end
为什么它省略了所有内部部分。
可能是因为我使用的扫描仪很少,但一切看起来都不错。
有什么建议吗?
更新:
感谢所有提供提示的人。我重新思考所有构造并用新信息重写代码。
为了避免棘手的扫描仪输入线,我使用了 JFileChooser
:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.JFileChooser;
public class KeepAskingApp {
private static int lines;
private static int words;
private static int chars;
public static void main(String[] args) {
boolean done = false;
// counters
int charsCount = 0, wordsCount = 0, linesCount = 0;
Scanner in = null;
Scanner lineScanner = null;
File selectedFile = null;
while (!done) {
try {
try {
JFileChooser chooser = new JFileChooser();
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
selectedFile = chooser.getSelectedFile();
in = new Scanner(selectedFile);
}
while (in.hasNextLine()) {
linesCount++;
lineScanner = new Scanner(in.nextLine());
lineScanner.useDelimiter(" ");
while (lineScanner.hasNext()) {
wordsCount++;
charsCount += lineScanner.next().length();
}
}
System.out.printf(
"# of chars: %d\n# of words: %d\n# of lines: %d\n",
charsCount, wordsCount, linesCount);
lineScanner.close();
lines += linesCount;
words += wordsCount;
chars += charsCount;
in.close();
} finally {
System.out.printf(
"\nAll lines: %d\nAll words: %d\nAll chars: %d\n",
lines, words, chars);
System.out.println("The end");
done = true;
}
} catch (FileNotFoundException e) {
System.out.println("Error! File not found.");
}
}
}
}
最佳答案
几个问题(实际上您的代码有很多问题,但我将解决与您发布的输出直接相关的问题):
首先,catch
block 中的内容仅在您收到 FileNotFoundException
时才会发生;这是用来处理错误并从错误中恢复的。我怀疑您打算在那里放置一个finally
block ,或者您打算在catch
之后执行此操作。我建议阅读 catching and handling exceptions 上的本教程,它直接描述了 try
、catch
和 finally
。
读完该教程后,请回到您的代码;您可能会发现需要进行一些重组。
其次,考虑到上述内容,从输出中可以明显看出您正在执行该 catch
block 中的代码,这意味着您将收到 FileNotFoundException
。这可能是由以下两个(可能是显而易见的)事情之一引起的:
找不到您输入的文件。它可能不存在,或者可能不在您期望的位置。检查并确保您输入了正确的文件名并且该文件确实存在。
input
字符串不是您所期望的。也许您从之前的输入中读取了一个空行等。
解决原因 2:如果由于某种原因输入缓冲区上已经存在换行符,您将使用 Scanner
读取一个空行。您可能想在打开文件之前打印 input
的值,以确保它符合您的预期。
如果您看到空行,请跳过它们。所以,而不是这个:
String input = in.nextLine();
scanner = new Scanner(new File(input));
像这样的东西将不受空行的影响:
String input;
do {
input = in.nextLine().trim(); // remove stray leading/trailing whitespace
} while (input.isEmpty()); // keep asking for input if a blank line is read
scanner = new Scanner(new File(input));
最后,我认为您可以找出输出中看到 0 的原因。当您尝试使用 new Scanner(new File(input));
打开文件时,由于找不到该文件而失败,它会抛出异常,程序立即跳转到中的代码你的catch block 。这意味着 lines
、words
和 chars
的初始值仍然为零(所有修改它们的代码都被跳过)。
希望有帮助。
关于java - 使用带有提示和用户输入的扫描仪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18288524/