Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用于反编译不使用数字的数学表达式的令牌迭代器类_Java_Iterator_Token - Fatal编程技术网

Java 用于反编译不使用数字的数学表达式的令牌迭代器类

Java 用于反编译不使用数字的数学表达式的令牌迭代器类,java,iterator,token,Java,Iterator,Token,我有一个类,我必须打印数学表达式的每个标记。我已经把代码打印出来了,除了数字。我不确定要修复什么才能做到这一点 这就是所需的输出: next token: [15] next token: [*] next token: [(] next token: [26] 这就是我的代码目前产生的结果: next token: [1] next token: [5] next token: [*] next token: [(] next token: [2] 等等 这

我有一个类,我必须打印数学表达式的每个标记。我已经把代码打印出来了,除了数字。我不确定要修复什么才能做到这一点

这就是所需的输出:

next token: [15] 

next token: [*] 

next token: [(] 

next token: [26] 
这就是我的代码目前产生的结果:

next token: [1] 

next token: [5] 

next token: [*] 

next token: [(] 

next token: [2] 
等等

这是我的密码:

public class TokenIter implements Iterator<String> {
    static int counter = 0;
    private String line;
    private String nextToken;

    public TokenIter(String line) {
        this.line = line;
    }

    public void remove() {
        throw new UnsupportedOperationException();
    }

    public static void main(String[] args) {
        String line = "  15*(26+37) - 443/7*612 - (233-543234)   ";
        System.out.println("line: [" + line + "]");
        TokenIter tokIt = new TokenIter(line);
        // print tokens in square brackets to check absence of white space
        while (tokIt.hasNext()) {
            if (tokIt.next() != "no")
                System.out.println("next token: [" + tokIt.next() + "]");
            counter++;
        }
    }

    public boolean hasNext() {
        if (counter < line.length())
            return true;
        else
            return false;
    }

    public String next() {
        String s = "";
        if (!hasNext())
            return null;
        else {
            char c = line.charAt(counter);
            if (c == ('/')) {
                counter = counter++;
                s = "" + c;
                return s;
            }
            if (c == ('+')) {
                counter = counter++;
                s = "" + c;
                return s;
            }
            if (c == ('*')) {
                counter = counter++;
                s = "" + c;
                return s;
            }
            if (c == ('-')) {
                counter = counter++;
                s = "" + c;
                return s;
            }
            if (c == ('(')) {
                counter = counter++;
                s = "" + c;
                return s;
            }
            if (c == (')')) {
                counter = counter++;
                s = "" + c;
                return s;
            } else if (c == ' ') {
                counter = counter++;
                return "no";
            }
            while (Character.isDigit(c)) {
                counter = counter++;
                s += "" + c;
                return s;
            }
        }
        return s;
    }
}

我知道还有其他方法可以做到这一点,比如带分隔符的字符串标记器,但我需要这样做。

好吧,您的代码包含几个问题:

如果是下一个没有

这不是比较字符串的正确方法。使用equals代替:if!tokIt.next.equalsno。您的当前值正确地比较了字符串,但找不到,因为您使用了。例如,如果用户在控制台中输入no,而您尝试使用!=不,记住这一点

你给托基特打了两次电话。第一次是在我上面提到的if语句中,第二次是在System.out.printlnext令牌:[+tokIt.next+];。如果您的代码通过增加计数器来正确移动光标,那么您将跳过每一秒标记。创建一个新字符串以携带令牌字符串token=tokIt.next;并使用它来比较它与否,并打印它

您不应该调用计数器++;在你的主要方法中。这会将光标移动到字符串中的下一个字符。把这个留给下一个方法。只有此方法才能移动光标

这不是问题,但您的hasNext可以简化为:

public boolean hasNext() {
    return counter < line.length();
}
所以如果你写counter=counter++;在向计数器中添加1后,将计数器设置为其旧值。这意味着,计数器不会改变其值。您可以删除计数器=部件,但是

在下一个方法中,不要在每个if分支中调用counter++。在char c=line.charAtcounter;中执行一次:

这样,您可以读取当前计数器的值并增加其值,因此下次读取某个值时,它将是下一个位置

不要创建变量s:这不是问题,但也没有帮助。因为每个if分支都包含

if (c == (...)) {
    s = "" + c;
    return s;
}
你可以改为写:

if (c == (...)) {
    return Character.toString(c);
}
这也避免了将字符转换为字符串:+c的尴尬版本

不要使用这些if语句进行简单检查,然后返回相同的字符。您可以这样做:

if ("/+*-()".indexOf(c) >= 0) {
    return Character.toString(c);
}
这样,您就可以在一个if语句中检查所有允许的数学字符。添加新的字符会更好

这是您当前的while循环,应读取每个组合数字:

while (Character.isDigit(c)) {
    counter = counter++;
    s += "" + c;
    return s;
}
如您所见,它将在第一次while循环迭代中返回s。您真正需要的是删除该返回,并从源字符串中读取下一个字符以将其添加到s。这可以是这样的:

String s = ""; // since we removed the old "s" (see my seventh remark), we need to create it here
while (Character.isDigit(c)) {
    s += Character.toString(c); // add current character to String s
    c = line.charAt(counter++); // read next character
    if (!Character.isDigit(c)) { // if it is not a digit
        counter--; // move the cursor one position back
        break; // stop the loop
    }
}
在您听了我的话之后,您的输出应该是:

line: [  15*(26+37) - 443/7*612 - (233-543234)   ]
next token: [15]
next token: [*]
next token: [(]
next token: [26]
next token: [+]
next token: [37]
next token: [)]
next token: [-]
next token: [443]
next token: [/]
next token: [7]
next token: [*]
next token: [612]
next token: [-]
next token: [(]
next token: [233]
next token: [-]
next token: [543234]
next token: [)]

我知道,有更好的方法来实现OPs目标,但我认为他仍然应该使用自己的方法,并从中学习。

@ali\m是的……我只是需要帮助,了解如何修复代码,使数字一致。请提供帮助。在我深入了解您的程序之前:if tokIt.next!=没有错。请阅读以下内容:。
String s = ""; // since we removed the old "s" (see my seventh remark), we need to create it here
while (Character.isDigit(c)) {
    s += Character.toString(c); // add current character to String s
    c = line.charAt(counter++); // read next character
    if (!Character.isDigit(c)) { // if it is not a digit
        counter--; // move the cursor one position back
        break; // stop the loop
    }
}
line: [  15*(26+37) - 443/7*612 - (233-543234)   ]
next token: [15]
next token: [*]
next token: [(]
next token: [26]
next token: [+]
next token: [37]
next token: [)]
next token: [-]
next token: [443]
next token: [/]
next token: [7]
next token: [*]
next token: [612]
next token: [-]
next token: [(]
next token: [233]
next token: [-]
next token: [543234]
next token: [)]