Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.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
为什么我的字符串变量没有被放入堆栈中? import java.util.*; 公共级Pemdas{ 公共静态双精度表达式(字符串str) { Stack num=新堆栈(); 堆栈op=新堆栈(); String number=“[0-9]*”;//0-9之间的任意数字 对于(int i=0;i_Java_Regex_Stack - Fatal编程技术网

为什么我的字符串变量没有被放入堆栈中? import java.util.*; 公共级Pemdas{ 公共静态双精度表达式(字符串str) { Stack num=新堆栈(); 堆栈op=新堆栈(); String number=“[0-9]*”;//0-9之间的任意数字 对于(int i=0;i

为什么我的字符串变量没有被放入堆栈中? import java.util.*; 公共级Pemdas{ 公共静态双精度表达式(字符串str) { Stack num=新堆栈(); 堆栈op=新堆栈(); String number=“[0-9]*”;//0-9之间的任意数字 对于(int i=0;i,java,regex,stack,Java,Regex,Stack,假设我有一个字符串变量“5+5”作为输入。在for循环中,5应该被推到num堆栈中,但我一直得到一个ESE,我不明白为什么。当您想与正则表达式匹配时,您使用的是equals()equals()用于比较文字字符串。您可能想要: 事实上,这里根本不需要正则表达式。您可以通过执行以下操作来简化循环: if (str.substring(i,i+1).matches(number)) num.push(Double.parseDouble(str.substring(i, i+1))); f

假设我有一个字符串变量“5+5”作为输入。在for循环中,5应该被推到num堆栈中,但我一直得到一个ESE,我不明白为什么。

当您想与正则表达式匹配时,您使用的是
equals()
equals()
用于比较文字字符串。您可能想要:


事实上,这里根本不需要正则表达式。您可以通过执行以下操作来简化循环:

if (str.substring(i,i+1).matches(number))
    num.push(Double.parseDouble(str.substring(i, i+1)));
for(int i=0;i

最后,请遵循Java的命名约定,当您要与正则表达式匹配时,请调用您的方法
express()
,而不是使用
equals()
equals()
用于比较文字字符串。您可能想要:


事实上,这里根本不需要正则表达式。您可以通过执行以下操作来简化循环:

if (str.substring(i,i+1).matches(number))
    num.push(Double.parseDouble(str.substring(i, i+1)));
for(int i=0;i


最后,请遵循Java的命名约定,调用您的方法
express()
,而不是
express()

“+”是否算作文本字符串,还是包含在正则表达式中?我在我的操作堆栈中尝试了同样的方法,但它也是空的。@igknighton作为
equals()
参数传递的所有内容都被视为文本字符串。好的,我的操作堆栈仍然被视为空的。是因为5s和++之间的空间吗?@igknighton我刚刚试过,效果很好,产生了正确的输出
10.0
。包括空格?它与“5+5”配合使用,但我无法让它与“5+5”配合使用。顺便问一下,我非常感谢您的帮助,“+”是作为文字字符串计算的,还是包含在正则表达式中?我在我的操作堆栈中尝试了同样的方法,但它也是空的。@igknighton作为
equals()
参数传递的所有内容都被视为文本字符串。好的,我的操作堆栈仍然被视为空的。是因为5s和++之间的空间吗?@igknighton我刚刚试过,效果很好,产生了正确的输出
10.0
。包括空格?它与“5+5”配合使用,但我无法让它与“5+5”配合使用。顺便说一句,我非常感谢你的帮助。
for (int i = 0; i < str.length(); i++)
{
    char c = str.charAt(i);

    if (Character.isDigit(c))            
        num.push((double) (c - '0'));

    else if (c == '+')         
        op.push("+");

    System.out.println(str);
}