无法读取以 JAVA 结尾的字符串

Unable to read string to end in JAVA

我正在尝试编写一个描述 JAVA 中的词法分析的程序。我能够定义令牌并设置所有内容,但我在读取 while 循环中输入的整个字符串时遇到问题。我希望字符串通过 do while 中的每一步,但我无法实现它们。如果我尝试不做,而显然它不会给出准确的结果。如何同时读到字符串末尾?

do {
            if (string.contains("+")) {
                lex_air[0]++;
            } else if (string.contains("-")) {
                lex_air[1]++;
            } else if (string.contains("*")) {
                lex_air[2]++;
            } else if (string.contains("/")) {
                lex_air[3]++;
            }
            if (string.contains(">")) {
                lex_log[0]++;
            } else if (string.contains("<")) {
                lex_log[1]++;
            } else if (string.contains("=")) {
                lex_log[2]++;
            } else if (string.contains(">=")) {
                lex_log[3]++;
            } else if (string.contains("<=")) {
                lex_log[4]++;
            }
            if (string.contains("for")) {
                lex_reserve[0]++;
            } else if (string.contains("int")) {
                lex_reserve[1]++;
            } else if (string.contains("while")) {
                lex_reserve[2]++;
            } else if (string.contains("char")) {
                lex_reserve[3]++;
            } else {
                lex_alpha++;
            }
        } while (string.endsWith(""));

试试这个:

String s = "";
        String[] words = s.split("\s+");
        for (int i = 0; i < words.length; i++) 
        {
            switch (words[i]) 
            {
            case  "+" :
                lex_air[0]++;
                break;

            case  "-":
                lex_air[1]++;
                break;

            case  "*" : 
                lex_air[2]++;
                break;

            case  "/":
                lex_air[3]++;
                break;

            case  ">":
                lex_log[0]++;
                break;

            case  "<":
                lex_log[1]++;
                break;

            case  "=":
                lex_log[2]++;
                break;

            case  ">=":
                lex_log[3]++;
                break;

            case  "<=":
                lex_log[4]++;
                break;

            case  "for":
                lex_reserve[0]++;
                break;

            case  "int":
                lex_reserve[1]++;
                break;

            case  "while":
                lex_reserve[2]++;
                break;

            case  "char":
                lex_reserve[3]++;
                break;

            default:
                lex_alpha++;
                break;

            }
        }