使用stringtokenizer构建扫描程序

时间:2016-12-17 23:30:29

标签: java stringtokenizer

我在这段代码中遇到问题,我试图在编译器过程中为我的项目构建一个扫描程序,扫描程序从用户那里获取任何输入并将其分成令牌。输出将是:print each令牌和它的类型(如:数字,标识符,关键字,加号......等),最后打印令牌数量。

我已声明了两个函数,一个用于确定输入是否为java保留关键字,第二个用于确定输入是否为标识符。

确定输入是否是java保留关键字的第一个函数运行良好,但我在第二个函数的声明中有一个问题,确定输入是否是标识符..当我输入一个保留关键字输出为:这是一个保留字,打印此输入多次为标识符。

这是我的代码:

package My_Scanner;
import java.util.Scanner;
import java.util.StringTokenizer;

public class MyScanner
{

    private static void  isOperator(String s)
    {
        if ( s.equals("+") )
        {
            System.out.print(s + "\t");
            System.out.println("Is Plus Sign");
        }

        else if ( s.equals("-") )
        {
            System.out.print(s + "\t");
            System.out.println("Is Minus Sign");
        }

        else if ( s.equals("*") )
        {
            System.out.print(s + "\t");
            System.out.println("Is Multiply Sign");
        }

        else if ( s.equals("/") )
        {
            System.out.print(s + "\t");
            System.out.println("Is Divide Sign");
        }

        else if ( s.equals("=") )
        {
            System.out.print(s + "\t");
            System.out.println("Is Assignment Operator");
        }

        else if ( s.equals("%") )
        {
            System.out.print(s + "\t");
            System.out.println("Is percent Operator");
        }

        else if ( s.equals(".") )
        {
            System.out.print(s + "\t");
            System.out.println("Is Full Stop");
        }
    }

    private static void  isReservedWord(String s)
    {
        for (int i = 0; i < reserved_Keywords.length; i++)
        {  
            if ( s.equals(reserved_Keywords[i]) )
            {  
                System.out.print(s + "\t");
                System.out.println("Is Reserved Keyword");
            }
        } 
    } 


    private static boolean isOpeningBracket(String s)
    {
        return (  s.equals("(") |  s.equals("{") | s.equals("[") |  s.equals("<")  ); 
    }

    private static boolean isClosingBracket(String s)
    {
        return (  s.equals(")") |  s.equals("}") | s.equals("]") |  s.equals(">")  ); 
    }

    private static void isIdentifier(String s)
    {
        boolean identifier = true;
        if ( !isOpeningBracket(s) && !isClosingBracket(s) )
        {
            for (int i = 0; i < reserved_Keywords.length; i++) {
                if ( s.equals(reserved_Keywords[i]) )
                {
                    identifier = false;
                    break;
                }
            }
        }

        if(identifier)
        {
            System.out.print(s + "\t");
            System.out.println("Is Identifier");
        }
    }

    static final 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", "main", "native",
            "new", "null", "package", "private", "protected", "public",
            "return", "short", "static", "strictfp", "string", "super", "switch",
            "synchronized", "this", "throw", "throws", "transient", "true",
            "try", "void", "volatile", "while" };

    public static void main(String[] args)
    {
        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.hasMoreTokens() )
        {
            String TOKEN = st.nextToken();

            isOperator(TOKEN);
            isReservedWord(TOKEN);
            isIdentifier(TOKEN);
            if ( isOpeningBracket(TOKEN) )
            {
                System.out.print(TOKEN + "\t");
                System.out.println("Is Left  Bracket");
            }
            else if ( isClosingBracket(TOKEN) )
            {
                System.out.print(TOKEN + "\t");
                System.out.println("Is Right  Bracket");
            }
        } 
        sc.close(); 
        System.out.println("Number of Tokens = " + numofTokens);
    }
}

1 个答案:

答案 0 :(得分:1)

那是因为else放置不正确。它会对所有元素进行检查,并且每次都在else中打印语句。相反,它需要在循环结束后只打印一次,尝试下面的代码:

private static void isIdentifier(String s) {
    boolean identifier = true;
    for (int i = 0; i < reserved_Keywords.length; i++) {
        if (s.equals(reserved_Keywords[i])) {
            identifier = false;
            break;
        }
    }

    if(identifier){
        System.out.print(s + "\t");
        System.out.println("Is Identifier");
    }
}

修改

要处理开始和结束括号,您可以在调用方法之前检查那些以检查标识符,如果找到任何这些字符,可以continue循环,例如:

while( st.hasMoreTokens() ){
    String TOKEN = st.nextToken();
    if ( isOpeningBracket(TOKEN) ){
        System.out.print(TOKEN + "\t");
        System.out.println("Is Left  Bracket");
        continue;
    }
    else if ( isClosingBracket(TOKEN) ){
        System.out.print(TOKEN + "\t");
        System.out.println("Is Right  Bracket");
        continue;
    }
    isOperator(TOKEN);
    isReservedWord(TOKEN);
    isIdentifier(TOKEN);
}