我是 Java 新手,正在学习它。我编写了一个代码来获取用户输入并对其进行标记。它编译成功,但运行时会产生“-3”作为 o/p。
import java.io.*;
public class TokenTester{
public static void main(String[] arguments){
String name;
StreamTokenizer token;
BufferedReader reader;
reader = new BufferedReader(new InputStreamReader(System.in));
try{
System.out.println("Enter a String ");
name = reader.readLine();
System.out.println(name);
token=new StreamTokenizer((new StringReader(name)));
//token=new StreamTokenizer(name);
// Using new StreamTokenizer(name) throws error
//error: no suitable constructor found for StreamTokenizer(String)
while(token.nextToken()!= token.TT_EOF)
{
System.out.println(token.nextToken());
}
}
catch (IOException ioe){
System.out.println("An unexpected error occured.");
}
}
}
此外,当我使用 new StreamTokenizer(name) 时会抛出错误“错误:找不到适合 StreamTokenizer(String) 的构造函数”
当我使用 while(token.nextToken()!= token.TT_EOF) 时,它显示无限循环为 -1 作为 o/p
下面的代码工作得很好:
import java.util.StringTokenizer;
class TokenTester {
public static void main(String[] arguments) {
StringTokenizer st1, st2;
String quote1 = "VIZY 3 -1/16";
st1 = new StringTokenizer(quote1);
System.out.println("Token 1: " + st1.nextToken());
System.out.println("Token 2: " + st1.nextToken());
System.out.println("Token 3: " + st1.nextToken());
String quote2 = "NPLI@9 27/32@3/32";
st2 = new StringTokenizer(quote2, "@");
System.out.println("\nToken 1: " + st2.nextToken());
System.out.println("Token 2: " + st2.nextToken());
System.out.println("Token 3: " + st2.nextToken());
}
}
我无法在我的代码中找出问题所在。请指导。
我参考过:
http://tutorials.jenkov.com/java-io/streamtokenizer.html
http://www.java-samples.com/showtutorial.php?tutorialid=397
http://docs.oracle.com/javase/7/docs/api/
谢谢高拉夫