gpt4 book ai didi

java - 使用带有提示和用户输入的扫描仪

转载 作者:行者123 更新时间:2023-12-04 04:45:24 29 4
gpt4 key购买 nike

我尝试从用户“输入”文件中计算行数、单词数、字符数。
在这个节目结束后,继续问。

如果文件不存在,则打印运行过程中统计过的所有数据。

代码:

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中的东东仅当您收到 FileNotFoundException 时才会阻止;用于处理错误并从错误中恢复。我怀疑你是想放一个 finally阻止那里,或者你打算在 catch 之后这样做.我建议在 catching and handling exceptions 上阅读本教程,它直接描述了 try , catch , 和 finally .

阅读该教程后,请回到您的代码;您可能会发现您需要进行一些重组。

其次,考虑到上述情况,您看到的输出很明显您正在执行 catch 中的代码。阻止,这意味着您将收到 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 中的代码。块。这意味着 lines , words , 和 chars它们的初始值仍然为零(所有修改它们的代码都被跳过)。

    希望有帮助。

    关于java - 使用带有提示和用户输入的扫描仪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18288524/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com