java—解析字符串并将字符串文本打印到屏幕

ff29svar  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(176)

我试图打印任何字符串文字,我在一个文件中,我正在阅读使用扫描方法。我只能按照教授的指导来学习我的课程结构。
目前,我有这段代码正在打印字符串文字,或者正在尝试:

public static void scan (String prog) {

            int n = prog.length();
            int index = 0;
            int lineNumber = 1;

            while (index < n) {

                char ch = prog.charAt(index);

                boolean whiteSpace = isWhiteSpace(ch);
                boolean newLine = isLineBreak(ch);
                TokenType sym = getSymbol(ch);
                TokenType op = getOp(ch);
                boolean letter = isLetter(ch);
                boolean digit = isDigit(ch);

                // start of if statement preceding this point               

                else if (ch == '\"') {
                    String str = "";
                    str += ch;

                    index++;

                        while (index < n) {

                            ch = prog.charAt(index);

                                if (ch != '\"') {

                                    str += ch;
                                    index++;
                                    }
                                else break; 
                            }

                        System.out.println(lineNumber + "," + TokenType.STRING + ", " + str);
                        index++;
                        continue;           
                }
                else {
                    System.out.println("Encountered something not expected: " + ch);
                    index++;
                    continue;
                }

这将输出第一个 " 以及下一个角色之前的所有角色 " 但不是最后一次 " 字符串文本本身。如何更改此代码以打印整个字符串文字?
电流输出:

11, STRING, "This just random code

我需要的是:

11, STRING, "This just random code"

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题