在Java中打印一段单词中的每个单词,同时使用Scanner类跳过一些单词
我正在写一个程序来接收一个文件并输出一个文件。 输入文件包含以下内容:在Java中打印一段单词中的每个单词,同时使用Scanner类跳过一些单词,java,java.util.scanner,nosuchelementexception,Java,Java.util.scanner,Nosuchelementexception,我正在写一个程序来接收一个文件并输出一个文件。 输入文件包含以下内容: 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 (程序在每行开头输入整数,然后跳过每行中的字数,然后保存其余的字并将其
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();
}
}
}
在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(scanner.hasNextLine())
{
StringTokenizer tokenizer=新的StringTokenizer(scanner.nextLine());
int count=Integer.parseInt(tokenizer.nextToken());
while(tokenizer.hasMoreTokens())
{
字符串b=tokenizer.nextToken()+“”;
如果(count嵌套的while和for循环中有一个bug,导致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 += " ";
}
这样,即使引发异常,扫描仪
和写入程序
也会自动关闭
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();
// 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 (Scanner scanner = new Scanner(file);
PrintWriter writer = new PrintWriter(args[1]);) {
scanner.next();
} catch (FileNotFoundException e) {
e.printStackTrace();
}