Java 使用带有提示和用户输入的扫描仪
我试图从用户“输入”文件中计算行数、单词数和字符数。Java 使用带有提示和用户输入的扫描仪,java,input,java.util.scanner,Java,Input,Java.util.scanner,我试图从用户“输入”文件中计算行数、单词数和字符数。 在这场表演结束后,继续数数并再次提问 如果文件不存在,则打印运行期间已计数的所有数据 代码: public class KeepAskingApp { private static int lines; private static int words; private static int chars; public static void main(String[] args) { boolean don
在这场表演结束后,继续数数并再次提问 如果文件不存在,则打印运行期间已计数的所有数据 代码:
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
为什么它省略了所有内部部分。可能是因为我用的扫描仪很少,但看起来都不错。 有什么建议吗 更新:
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;
}
}
}
谢谢所有给你提示的人。我重新思考所有构造的代码,并用新信息重写代码。为了避开棘手的扫描仪输入行,我使用了
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.");
}
}
}
}
您的println()
s位于catch
块中
} 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;
}
这意味着您捕获了一个FileNotFoundException
。我想您可以从这里找到答案。两个问题(实际上您的代码有很多问题,但我将解决与您发布的输出直接相关的问题):
首先,catch
块中的内容只有在获得FileNotFoundException
时才会发生;这是用来处理和恢复错误的。我怀疑你是想在那里放置一个最后
块,或者你是想在捕获
之后这样做的。我建议您在上阅读本教程,它直截了当地描述了try
,catch
,以及finally
一旦你阅读了教程,回到你的代码;你可能会发现你有一点重组要做
其次,记住以上内容,从输出中可以明显看出,您正在执行catch
块中的代码,这意味着您将获得FileNotFoundException
。这可能是由以下两种(可能很明显)原因之一造成的:
输入
字符串不是您所期望的。也许您从以前的输入中读取了一个空行,等等扫描仪
读取一个空行。您可能希望在打开文件之前打印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的原因。当您尝试使用新扫描仪(新文件(输入))打开文件时代码>失败,因为它找不到文件,它抛出异常,程序立即跳转到catch
块中的代码。这意味着行
,字
和字符
的初始值仍然为零(所有修改它们的代码都被跳过)
希望能有所帮助。这是一段糟糕的代码,原因很多,会把你搞得一团糟:words+=wordscont++代码>但不是数字为0的原因。您应该将此代码放在单独的行中。你应该在计算完这行的所有单词后添加单词。我做了一些重构。用input
将这个棘手的时刻更改为JFileChooser
,但它一直抛出未找到文件
并关闭?我刚刚尝试了你的更新版本(现在看起来好多了,文件选择器是个好主意)。我用文件选择器选择了一个文本文件,它打开后运行良好。FileNotFoundException的错误消息是什么?此外,您应该在问题中发布更新后的代码,以便其他可能愿意提供帮助的人知道它。:)我解决了这个问题,因为我使用的是Ubuntu,没有足够的权限。但它应该继续要求下一个输入文件,并且只有当您给出错误的文件(不存在)时,才给出所有反向数据的最终部分。。。