我在这段代码中遇到问题,我正在尝试在编译器类(class)中为我的项目构建一个扫描仪,扫描仪接受用户的任何输入并将其分成 token ..输出将是:打印每个 token 并它是类型(如:数字、标识符、关键字、加号...等),最后打印 token 的数量。
我尝试了更多输入,每次输出都是标识符,当我尝试输入数字或关键字或+或-时,输出是标识符..
这是我的代码:
import java.util.Scanner;
import java.util.StringTokenizer;
public class MyScanner
{
public static void main(String[] args)
{
String reserved_Keywords[] = { "abstract", "assert", "boolean",
"break", "byte", "case", "catch", "char", "class", "const",
"continue", "default", "do", "double", "else", "extends", "false",
"final", "finally", "float", "for", "goto", "if", "implements",
"import", "instanceof", "int", "interface", "long", "native",
"new", "null", "package", "private", "protected", "public",
"return", "short", "static", "strictfp", "super", "switch",
"synchronized", "this", "throw", "throws", "transient", "true",
"try", "void", "volatile", "while" };
Scanner sc = new Scanner(System.in);
System.out.println("Enter Your Text: ");
String str = sc.nextLine();
StringTokenizer st = new StringTokenizer(str);
int numofTokens = st.countTokens();
while( st.hasMoreElements() )
{
for (int i = 0; i < reserved_Keywords.length; i++)
{
if ( st.equals(reserved_Keywords[i]) )
{
System.out.print(st.nextElement() + "\t");
System.out.println("Is Reserved Keyword");
}
}
if ( st.equals("+") )
{
System.out.print(st.nextElement() + "\t");
System.out.println("Is Plus Sign");
}
else if ( st.equals("-") )
{
System.out.print(st.nextElement() + "\t");
System.out.println("Is Minus Sign");
}
else if ( st.equals("*") )
{
System.out.print(st.nextElement() + "\t");
System.out.println("Is Multiply Sign");
}
else if( st.equals("/") )
{
System.out.print(st.nextElement() + "\t");
System.out.println("Is Divide Sign");
}
else if ( st.equals("=") )
{
System.out.print(st.nextElement() + "\t");
System.out.println("Is Assignment Operator");
}
else
{
System.out.print(st.nextElement() + "\t");
System.out.println("Is Identifier");
}
}
sc.close();
System.out.println("Number of Tokens = " + numofTokens);
}
}
最佳答案
您始终将(调用 equals(..)( )与 StringTokenizer 进行比较,而不是与 StringTokenizer 返回的 token 进行比较。
要解除此问题,请添加 while 循环的第一行
String TOKEN = st.nextToken();
然后将所有比较(调用 equals())替换为 st 而不是 TOKEN。
(当然,您应该命名变量,而不是使用大写字母,我这样做只是为了便于阅读)
那么你的代码将如下所示:
StringTokenizer st = new StringTokenizer(str);
int numofTokens = st.countTokens();
while( st.hasMoreElements() )
{
String TOKEN = st.nextToken();
for (int i = 0; i < reserved_Keywords.length; i++)
{
if ( TOKEN.equals(reserved_Keywords[i]) )
{
System.out.print(st.nextElement() + "\t");
System.out.println("Is Reserved Keyword");
}
}
...
关于java - 如何使用 stringtokenizer 构建扫描器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41184637/