当前位置: 代码迷 >> Java相关 >> java.lang.outmemoryerror
  详细解决方案

java.lang.outmemoryerror

热度:797   发布时间:2007-01-11 01:46:37.0
java.lang.outmemoryerror

请问这是什么错误? 我编译已经通过了。。。

import java.util.Stack;
import java.io.*;
public class addlongnum{

public Stack ran(){ 输入一些数存到堆栈中,并且返回这个堆栈
Stack a= new Stack();
try{

char num=(char)System.in.read();
while (num!='\n'){

a.push(new Character(num)) ;}
} catch (Exception e){}
return a;
}

public void statues(Stack a){ 把堆栈中的元素都打印出来
while (a.isEmpty()==false){
System.out.print(a.pop()+" ");
}
}


public Stack addlongnum(Stack a,Stack b){//长整数加法 就是把a,b两个堆栈中的数逐一取出相加然后存在另一个堆栈中
Object tem=new Object();
Object tema=new Object();
Object temb=new Object();
int d=0;
Stack stack=new Stack();
for(;a.isEmpty()==false&&b.isEmpty()==false;){
tema=a.pop();temb=b.pop();
int c = Integer.parseInt(tema.toString())+Integer.parseInt(temb.toString())+d;
if(c<=9)
d=0;
else
{
d=1;
c=c-10;
}
stack.push(new Integer(c));
}
for(;a.isEmpty()==false;){
if(d==1){
tema=a.pop();
int e=Integer.parseInt(tema.toString())+d;
stack.push(new Integer(e));
d=0;
}
else
{
stack.push(a.pop());
}
}
for(;b.isEmpty()==false;){
if(d==1){
temb=b.pop();
int e=Integer.parseInt(temb.toString())+d;
stack.push(new Integer(e));
d=0;
}
else
{
stack.push(b.pop());
}

}

return stack;
}

public static void main(String arg[]) {
Stack a=new Stack();
Stack b=new Stack();

addlongnum xiaohe =new addlongnum();
System.out.println("enter a");
a=xiaohe.ran(); 显示到这就弹出错误!!!
System.out.println("enter b");
b=xiaohe.ran();
xiaohe.statues(xiaohe.addlongnum(a,b));
}
}

[此贴子已经被作者于2007-1-11 1:53:50编辑过]

搜索更多相关的解决方案: public  return  import  color  

----------------解决方案--------------------------------------------------------
不好意思,发现 public Stack ran(){ 输入一些数存到堆栈中,并且返回这个堆栈
Stack a= new Stack();
try{

char num=(char)System.in.read();
while (num!='\n'){

a.push(new Character(num)) ;
num=(char)System.in.read(); } 这儿少了一句
} catch (Exception e){}
return a;
} 不过运行后还是错误,不能将两个数相加。。。

----------------解决方案--------------------------------------------------------
你递归调用了吧,要么怎么会出现内存溢出的异常呢
你的代码我没有看,你再检查一遍吧
----------------解决方案--------------------------------------------------------
解决方法。。。。
try{
c = Integer.parseInt(tema.toString())+Integer.parseInt(temb.toString())+d;

}catch(NumberFormatException nfe){}
----------------解决方案--------------------------------------------------------
  相关解决方案