StringTokenizer error: NoSuchelementException after tokenized line finishes
StringTokenizer error: NoSuchelementException after tokenized line finishes
当我从键盘向分词器输入一行时,它没有问题。然而,
我试图通过文件行提供我的输入,文件行的名称在两个词之间有一个 space。那是我收到 NoSuchelementException 错误的时候。我不确定是否需要关闭文件?但该文件有更多信息需要阅读。感谢您花时间阅读我的问题:)
代码如下:
System.out.println("Enter your first and last name");
line = sc.nextLine();
System.out.println(line);
StringTokenizer lineTok = new StringTokenizer(line);
first = (String) lineTok.nextToken();
last = (String) lineTok.nextToken();
对我来说效果很好。
package tryouts;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.StringTokenizer;
public class StringTokenizerTest {
public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(new FileInputStream("/usr/projects/tryouts/src/tryouts/test.txt"));
String line = sc.nextLine();
System.out.println(line);
StringTokenizer lineTok = new StringTokenizer(line);
String first = (String) lineTok.nextToken();
System.out.println(first);
String last = (String) lineTok.nextToken();
System.out.println(last);
sc.close();
}
}
在文件夹“/usr/projects/tryouts/src/tryouts/”中创建一个名为"test.txt"的文件,单行内容为"first second third fourth"并打印第一和第二。
下面的将打印该行中的所有单词。
package tryouts;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.StringTokenizer;
public class StringTokenizerTest {
public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(new FileInputStream(
"/usr/projects/tryouts/src/tryouts/test.txt"));
String line = sc.nextLine();
System.out.println(line);
StringTokenizer lineTok = new StringTokenizer(line);
while (lineTok.hasMoreTokens()) {
System.out.println(lineTok.nextToken());
}
sc.close();
}
}
当我从键盘向分词器输入一行时,它没有问题。然而, 我试图通过文件行提供我的输入,文件行的名称在两个词之间有一个 space。那是我收到 NoSuchelementException 错误的时候。我不确定是否需要关闭文件?但该文件有更多信息需要阅读。感谢您花时间阅读我的问题:)
代码如下:
System.out.println("Enter your first and last name");
line = sc.nextLine();
System.out.println(line);
StringTokenizer lineTok = new StringTokenizer(line);
first = (String) lineTok.nextToken();
last = (String) lineTok.nextToken();
对我来说效果很好。
package tryouts;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.StringTokenizer;
public class StringTokenizerTest {
public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(new FileInputStream("/usr/projects/tryouts/src/tryouts/test.txt"));
String line = sc.nextLine();
System.out.println(line);
StringTokenizer lineTok = new StringTokenizer(line);
String first = (String) lineTok.nextToken();
System.out.println(first);
String last = (String) lineTok.nextToken();
System.out.println(last);
sc.close();
}
}
在文件夹“/usr/projects/tryouts/src/tryouts/”中创建一个名为"test.txt"的文件,单行内容为"first second third fourth"并打印第一和第二。
下面的将打印该行中的所有单词。
package tryouts;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.StringTokenizer;
public class StringTokenizerTest {
public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(new FileInputStream(
"/usr/projects/tryouts/src/tryouts/test.txt"));
String line = sc.nextLine();
System.out.println(line);
StringTokenizer lineTok = new StringTokenizer(line);
while (lineTok.hasMoreTokens()) {
System.out.println(lineTok.nextToken());
}
sc.close();
}
}