Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.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 - Fatal编程技术网

Java 查找括号内的数字

Java 查找括号内的数字,java,Java,作为我的项目的一部分,我需要能够在这些括号内添加数字,我已经工作了太久,但没有结果,我将感谢任何帮助 String[] s = "1 2 3 ( 4 5 6 ) * 1000 7 8".split("\\s"); 例如,本例中有3个数字 这就是我到目前为止所做的: while(st.hasMoreTokens()) { if ( st.nextToken() != "(" ) count = count +1 ; } if ( st.nextToken() == "("

作为我的项目的一部分,我需要能够在这些括号内添加数字,我已经工作了太久,但没有结果,我将感谢任何帮助

String[] s = "1 2 3 ( 4 5 6 ) * 1000 7 8".split("\\s");
例如,本例中有3个数字

这就是我到目前为止所做的:

while(st.hasMoreTokens()) {
    if ( st.nextToken() != "(" )
        count = count +1 ;
}
if ( st.nextToken() == "(" )
    while (st.nextToken() != ")") {
        count2 = count2 + 1;
    }
System.out.println(count); System.out.println(count2);
}

没有完全理解这里的用例。假设你总是只有一组偏执[“(“and”)”],那么就不会有偏执不匹配,你也不关心偏执之外的其他数字/数字/运算符,在偏执之间添加数字的简单方法是

String mainStr = "1 2 3 ( 4 5 6 ) * 1000 7 8";
String []inside = mainStr.substring(mainStr.indexOf("(")+1, mainStr.indexOf(")")
                  .split("\s");
int sum = 0;
int innerNum = 0;
for (int i=0; i<inside.length;i++) {
    try {
        innerNum = Integer.parseInt(inside[i]);
    } catch (NumberFormatException e) {
        innerNum = 0;
    }
    sum = sum + innerNum; 
}
return sum;
String-mainStr=“1 2 3(4 5 6)*1000 7 8”;
String[]inside=mainStr.substring(mainStr.indexOf(“(”)+1,mainStr.indexOf(“))
.split(“\s”);
整数和=0;
int innernnum=0;

对于(int i=0;i如果您想跟踪括号并将字符串作为等式求值,您可能需要查看调车场算法。

向我们展示您迄今为止所做的工作。我们允许您首先自己尝试这样做。最终,我需要将所有这些数字相加,但括号内的数字将为*1000在这种情况下,while(st.hasMoreTokens()){if(st.nextToken()!=”(“”)count=count+1;}if(st.nextToken()=”(“”)while(st.nextToken()!=”)while(st.hasMoreTokens()){count2=count2+1;}System.out.println(count2);}@maniforoozi-您发布的代码中包含的大括号
{
,不包括您的
st
声明。我已将您的代码粘贴到您的问题中,请使用您正在使用的实际代码更新它,包括
st