打印 Java 中一段文字中的每个单词,同时使用 Scanner class 跳过一些单词
Printing each word from a paragraph of words in Java while skipping some using Scanner class
我正在编写一个程序来接收一个文件并输出一个文件。
输入文件包含如下内容:
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();
}
}
}
而且这些代码工作得很好。
但是,现在我想从使用拆分方法切换到另一个第二个扫描仪 class 来读取每个句子到每个单词。
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
我觉得这个错误消息是由于第二台扫描仪 class 没有正确关闭。但是我加了
之后也没弄明白怎么解决
s2.close();
在for循环中。
感谢任何帮助。谢谢,但我对 Java、
真的很陌生
例如,您可以使用 StringTokenizer 而不是第二个扫描器:
while(scanner.hasNextLine())
{
StringTokenizer tokenizer = new StringTokenizer(scanner.nextLine());
int count = Integer.parseInt(tokenizer.nextToken());
while(tokenizer.hasMoreTokens())
{
String b = tokenizer.nextToken() + " ";
if(count <= 0)
{
c += b;
}
count--;
}
}
scanner.close();
我认为你的第二个扫描器的问题是内循环和扫描器位置 - 你想增加它的字数并且只添加一些到你的输出字符串。
嵌套的 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();
}
这样,即使抛出异常,scanner
和 writer
也会自动关闭。
我正在编写一个程序来接收一个文件并输出一个文件。 输入文件包含如下内容:
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();
}
}
}
而且这些代码工作得很好。 但是,现在我想从使用拆分方法切换到另一个第二个扫描仪 class 来读取每个句子到每个单词。
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
我觉得这个错误消息是由于第二台扫描仪 class 没有正确关闭。但是我加了
之后也没弄明白怎么解决s2.close();
在for循环中。
感谢任何帮助。谢谢,但我对 Java、
真的很陌生例如,您可以使用 StringTokenizer 而不是第二个扫描器:
while(scanner.hasNextLine())
{
StringTokenizer tokenizer = new StringTokenizer(scanner.nextLine());
int count = Integer.parseInt(tokenizer.nextToken());
while(tokenizer.hasMoreTokens())
{
String b = tokenizer.nextToken() + " ";
if(count <= 0)
{
c += b;
}
count--;
}
}
scanner.close();
我认为你的第二个扫描器的问题是内循环和扫描器位置 - 你想增加它的字数并且只添加一些到你的输出字符串。
嵌套的 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();
}
这样,即使抛出异常,scanner
和 writer
也会自动关闭。