java - 如何使用 stringtokenizer 构建扫描器

标签 java eclipse compiler-construction stringtokenizer

我在这段代码中遇到问题,我正在尝试在编译器类(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/

相关文章:

java - 如何在 Tomcat 8 中创建一个新的共享文件夹并从中加载我的属性文件

java - 如何在没有 ChromeDriver.exe 的情况下从 Selenium Remote Webdriver 启动 Google Chrome

java - 将更改从 Bitbucket 更新到 Eclipse 中的本地 git 存储库?

Java 已启动但返回退出代码 = 13

c++ - int * array = new int [size](); 的有效性

java - 为动态 Web 项目配置 JPA 提供程序

java - Eclipse 中的重构工具

java - 如何在 Eclipse 中添加 Java 项目而不是 Jar

c# - C# 编译器是开源的吗?

c++ - 如何查看编译器生成的代码