我正在编写一个程序来接收文件并输出文件。 输入文件包含如下内容:
1 cat dog rabbit
3 cat dog rabbit rabbit rabbit
2 yellow red blue white black
0 three two one
输出文件为:
dog rabbit rabbit rabbit blue white black three two one
(程序接受每行开头的整数,然后跳过每行的单词数,然后保存其余单词并输出到文件)
最初,我有
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.File;
import java.io.PrintWriter;
public class Scanner2{
public static void main(String args[]) {
String c = "";
try{
File file = new File(args[0]);
Scanner scanner = new Scanner(file);
PrintWriter writer = new PrintWriter(args[1]);
// as long as the scanner reads that the file has a next line
while (scanner.hasNextLine()) {
// read the next line of string as string s
String s = scanner.nextLine();
// split each word from the string s as an array called "words"
String[] words = s.split(" ");
// for loop executed length of "words" times
for(int x = 0; x < words.length; x++) {
// declare int, "count"
int count;
// parse the first element (the number) from "words" to be an integer, "count"
count = Integer.parseInt(words[0]);
// if the loop is executed more than "count" number of times
if (x > count){
// add respective element to string, "c"
c += words[x];
c += " ";
}
}
}
// close the scanner
scanner.close();
// output string "c" to the output file
writer.println(c);
// close the writer
writer.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
这些代码工作得很好。 但是,现在我想从使用 split 方法切换到另一个第二个扫描器类来读取每个句子的每个单词。
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.File;
import java.io.PrintWriter;
public class ScannerDemo{
public static void main(String args[]) {
String c = "";
try{
File file = new File(args[0]);
Scanner scanner = new Scanner(file);
PrintWriter writer = new PrintWriter(args[1]);
// as long as the scanner reads that the file has a next line
while (scanner.hasNextLine()) {
// read the first line of string in scanner s2
Scanner s2 = new Scanner(scanner.nextLine());
// read the first word of first line from s2 as string "counts"
String counts = s2.next();
// parse the string "counts" as int, "count"
int count = Integer.parseInt(counts);
// as long as s2 has the next element
while (s2.hasNext()){
// for loop executed "count" number of times to skip the words
for (int x = 0; x < count; x ++){
String b = s2.next();
}
// else add the next words to string, "c"
c += s2.next();
c += " ";
}
}
// close the scanner
scanner.close();
// output string "c" to the output file
writer.println(c);
// close the writer
writer.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
但是,它给出了错误消息
线程“main”中的异常java.util.NoSuchElementException
我认为此错误消息是由于第二个扫描仪类未正确关闭所致。但是我添加后并没有弄清楚如何解决
s2.close();
在 for 循环中。
感谢任何帮助。谢谢,但我对 Java 真的很陌生,
最佳答案
嵌套的 while 和 for 循环中存在一个错误,导致 for 循环中的 s2.next() 超出行尾。尝试如下
// parse the string "counts" as int, "count"
int count = Integer.parseInt(counts);
// for loop executed "count" number of times to skip the words
for (int x = 0; x < count && s2.hasNext(); x++){
String b = s2.next();
}
// as long as s2 has the next element add the next words to string
while (s2.hasNext()) {
c += s2.next();
c += " ";
}
此外,建议使用try with resources
而不是自行关闭。简化示例:
try (Scanner scanner = new Scanner(file);
PrintWriter writer = new PrintWriter(args[1]);) {
scanner.next();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
这样,即使抛出异常,扫描器
和编写器
也会自动关闭。
关于java - 在 Java 中打印一段单词中的每个单词,同时使用 Scanner 类跳过一些单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32417928/