编译通过,运行时却总抛出error

dakjhflkjdsfk 2016-11-01 06:01:57
下面这段代码编译是通过的,运行时却总抛出下面的error,这是为什么呢
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Unknown Source)
at java.lang.AbstractStringBuilder.ensureCapacityInternal(Unknown Source)
at java.lang.AbstractStringBuilder.append(Unknown Source)
at java.lang.StringBuffer.append(Unknown Source)
at practice.Expression.toPostfix(Expression.java:24)
at practice.Expression.main(Expression.java:96)


public class Expression
{
public static StringBuffer toPostfix(String infix)
{
Stack<String> stack = new SeqStack<String>(infix.length());
StringBuffer postfix = new StringBuffer(infix.length()*2);
int i = 0;
while(i<infix.length())
{
char ch = infix.charAt(i);
switch(ch)
{
case'+': case'-':
while(!stack.isEmpty()&&!stack.peek().equals("("))
postfix.append(stack.pop());
stack.push(ch+"");
i++; break;

case'*': case'/':
while(!stack.isEmpty()&&(stack.peek().equals("*")||
stack.peek().equals("/")))
postfix.append(ch);
stack.push(ch+"");
i++; break;

case'(':
stack.push(ch+"");
i++; break;

case')':
String out = stack.pop();
while(out!=null&&!out.equals("("))
{
postfix.append(out);
out = stack.pop();
}
i++; break;

default:
while(i<infix.length()&&ch>='0'&&ch<='9')
{
postfix.append(ch);
i++;
if(i<infix.length())
ch = infix.charAt(i);
}
postfix.append(" ");
}
}
while(!stack.isEmpty())
postfix.append(stack.pop());
return postfix;

}

public static int toValue(StringBuffer postfix)
{
Stack<Integer> stack = new LinkedStack<Integer>();
int value = 0;
for(int i=0; i<postfix.length(); i++)
{
char ch = postfix.charAt(i);
if(ch>='0'&&ch<='9')
{
value = 0;
while(ch!=' ')
{
value = value*10+ch-'0';
ch = postfix.charAt(++i);
}
stack.push(value);
}
else
if(ch!=' ')
{
int y = stack.pop(), x = stack.pop();
switch(ch)
{
case'+': value = x+y; break;
case'-': value = x-y; break;
case'*': value = x*y; break;
case'/': value = x/y; break;
}
//System.out.print(x+(ch+"")+y+"="+value+",");
stack.push(value);
}
}
return stack.pop();
}
public static void main(String[] args)
{
// TODO 自动生成的方法存根
String infix = "123+10*(45-50+20)/((35-25)*2+10)-11";
StringBuffer postfix = toPostfix(infix);
System.out.println("infix="+infix+"\npostfix="+postfix
+"\nvalue="+toValue(postfix));
}

}

...全文
334 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
DAVE-BLACK 2019-12-05
  • 打赏
  • 举报
回复
你加内存啊。。。。。。。。
weixin_44766586 2019-12-03
  • 打赏
  • 举报
回复
case '*': case '/': while (!stack.isEmpty() && (stack.peek().equals("*") || stack.peek() .equals("/"))) postfix.append(stack.pop()); stack.push(ch + ""); i++; break; postfix.append(stack.pop());這一行錯了
qq_29593929 2016-11-09
  • 打赏
  • 举报
回复
不加{}出错真的好读么
Be_nurturing 2016-11-09
  • 打赏
  • 举报
回复
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space 这一句都已经说了
Smiletotakeyoufly 2016-11-08
  • 打赏
  • 举报
回复
看报错提示就可以,OutOfMemoryError 内存溢出了
  • 打赏
  • 举报
回复
目测有死循环
dakjhflkjdsfk 2016-11-01
  • 打赏
  • 举报
回复
天哪噜.....我居然深陷其中,死活没发现 谢谢!!!
落雨尘封 2016-11-01
  • 打赏
  • 举报
回复
case'*': case'/': while(!stack.isEmpty()&&(stack.peek().equals("*")|| stack.peek().equals("/"))) postfix.append(ch); stack.push(ch+""); i++; break; 这里死循环了吧,一直append然后就oom了 {}没加?

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧