LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-17-2011, 08:03 PM   #1
twindesert
LQ Newbie
 
Registered: Nov 2011
Posts: 5

Rep: Reputation: Disabled
Exception in thread "main" java.lang.StackOverflowError


Hello, when I run the following code I get the error Exception in thread "main" java.lang.StackOverflowError (Main.java:59) and I don't know how to fix it.

public class Main{

public static int r = 0;
public static int n;

public static final int i = 4;
public static final int j = 4;
public static int x = 0;
public static int y = 0;
public static boolean simetrica = true;
public static int [][] m = new int[i][j];

public static int tmp = 0;
public static boolean simetricaSuRaizCuadradaEntera = true;

public static void main(String[] args) {

n = 10;

m[0][0]=1;
m[1][0]=10;
m[2][0]=25;
m[3][0]=5;
m[0][1]=9;
m[1][1]=1;
m[2][1]=64;
m[3][1]=2;
m[0][2]=25;
m[1][2]=64;
m[2][2]=1;
m[3][2]=4;
m[0][3]=5;
m[1][3]=2;
m[2][3]=4;
m[3][3]=50;

Main prueba = new Main();

prueba.raizCuadradaEntera(n);
System.out.println(r);
prueba.esSimetrica(m);
System.out.println(simetrica);
prueba.esSimetricaSuRaizCuadradaEntera(m);
System.out.println(simetricaSuRaizCuadradaEntera);

}

public int raizCuadradaEntera(int n){

if (n == 0){
return n;
}else{
if ( (n >= (r*r)) && (n < (r+1)*(r+1)) ){
return(r);
}else{
r++;
return(raizCuadradaEntera(n));
}
}

}

public boolean esSimetrica(int [][] m){

if (m.length != m[0].length){
return false;
}
if (x != m.length - 1){
if (y == m[x].length - 1){
if (m[x][y] != m[y][x]){
simetrica = false;
}
x++;
y = 0;
esSimetrica(m);
}else{
if (m[x][y] != m[y][x]){
simetrica = false;
}else{
y++;
esSimetrica(m);
}
}
}

return simetrica;
}

public boolean esSimetricaSuRaizCuadradaEntera(int[][] m){

calcularRaices(m);
esSimetrica(m);
simetricaSuRaizCuadradaEntera = simetrica;
return simetricaSuRaizCuadradaEntera;
}

private int[][] calcularRaices(int [][] m){

if (x != m.length - 1){
if (y == m[x].length - 1){
n = m[x][y];
m[x][y] = raizCuadradaEntera(n);
x++;
y = 0;
calcularRaices(m);
}else{
n = m[x][y];
m[x][y] = raizCuadradaEntera(n);
y++;
calcularRaices(m);
}
}
return m;
}

}

3
false
Exception in thread "main" java.lang.StackOverflowError
at ....(Main.java:59)


Thanks for any suggestions
 
Old 11-18-2011, 03:12 AM   #2
aspire1
Member
 
Registered: Dec 2008
Distribution: Ubuntu
Posts: 62

Rep: Reputation: 23
Looks like the none of the end conditions for the recursive call are being met
so you keep calling raizCudradaEntera with the same unadjusted value for n until the
stackoverflows.



Code:
public int raizCuadradaEntera(int n){

if (n == 0){
   return n;
   }else{
   
 if ( (n >= (r*r)) && (n < (r+1)*(r+1)) ){
   return(r);
  }else{
  
 r++;

 return(raizCuadradaEntera(n));
}
}

}
 
Old 11-18-2011, 05:08 AM   #3
twindesert
LQ Newbie
 
Registered: Nov 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thanks for your post.
I made some modification, but my level of programming isn't very good and I haven't fixed the error. If you could help me I would appreciate that.

Thanks for any suggestions

public class Main{

public static int r = 0;
public static int n;

public static final int i = 4;
public static final int j = 4;
public static int x = 0;
public static int y = 0;
public static boolean simetrica = true;
public static int [][] m = new int[i][j];

public static int tmp = 0;
public static boolean simetricaSuRaizCuadradaEntera = true;

public static void main(String[] args) {

n = 10;

m[0][0]=1;
m[1][0]=10;
m[2][0]=25;
m[3][0]=5;
m[0][1]=9;
m[1][1]=1;
m[2][1]=64;
m[3][1]=2;
m[0][2]=25;
m[1][2]=64;
m[2][2]=1;
m[3][2]=4;
m[0][3]=5;
m[1][3]=2;
m[2][3]=4;
m[3][3]=50;

Main prueba = new Main();

prueba.raizCuadradaEntera(n);
System.out.println(r);
prueba.esSimetrica(m);
System.out.println(simetrica);
prueba.esSimetricaSuRaizCuadradaEntera(m);
System.out.println(simetricaSuRaizCuadradaEntera);

}

public int raizCuadradaEntera(int n){

if (n == 0){
return n;
}else{
if ( (n >= (r*r)) && (n < (r+1)*(r+1)) ){
return(r);
}else{
r++;
raizCuadradaEntera(n);
}
}
return(r);

}

public boolean esSimetrica(int [][] m){

if (m.length != m[0].length){
return false;
}
if (x != m.length - 1){
if (y == m[x].length - 1){
if (m[x][y] != m[y][x]){
simetrica = false;
}
x++;
y = 0;
esSimetrica(m);
}else{
if (m[x][y] != m[y][x]){
simetrica = false;
}else{
y++;
esSimetrica(m);
}
}
}

return simetrica;
}

public boolean esSimetricaSuRaizCuadradaEntera(int[][] m){

calcularRaices(m);
esSimetrica(m);
simetricaSuRaizCuadradaEntera = simetrica;
return simetricaSuRaizCuadradaEntera;
}

private int[][] calcularRaices(int [][] m){

if (x != m.length - 1){
if (y == m[x].length - 1){
n = m[x][y];
raizCuadradaEntera(n);
m[x][y] = r;
x++;
y = 0;
calcularRaices(m);
}else{
n = m[x][y];
raizCuadradaEntera(n);
m[x][y] = r;
y++;
calcularRaices(m);
}
}
return m;
}

}
 
Old 11-18-2011, 06:13 AM   #4
aspire1
Member
 
Registered: Dec 2008
Distribution: Ubuntu
Posts: 62

Rep: Reputation: 23
Code:
public int raizCuadradaEntera(int n){

if (n == 0){
return n;
}else{
if ( (n >= (r*r)) && (n < (r+1)*(r+1)) ){  // This line
return(r);
}else{
r++;
raizCuadradaEntera(n);
}
}
return(r);

}
Running your code: eventually n=5 and r=5, you test will never be true
from now on. You increment r then call raizCuadradaEntera(5);
Now n=5 and r=6
The test obviously fails, you increment
r then call raizCuadradaEntera(5);
n=5 now and r = 7
and so on stuck in the recursive call.

Modify the conditions and variable adjusts so
that the you can get out of the recusrion.
 
1 members found this post helpful.
Old 11-18-2011, 06:38 AM   #5
twindesert
LQ Newbie
 
Registered: Nov 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thank you very much! : D
I think I've managed to fix.
I have another problem .... : S

m [0] [0] = 1;
m [1] [0] = 10 / / would have to be 9 to be symmetrical, but applying the serious raizCuadradaEntera = 3, then [1] [0] = [0] [1] and would be true, but it comes out false
m [2] [0] = 25;
m [3] [0] = 5;
m [0] [1] = 9 -> 3
m [1] [1] = 1;
m [2] [1] = 64;
m [3] [1] = 2;
m [0] [2] = 25;
m [1] [2] = 64;
m [2] [2] = 1;
m [3] [2] = 4;
m [0] [3] = 5;
m [1] [3] = 2;
m [2] [3] = 4;
m [3] [3] = 50;
 
Old 11-18-2011, 06:43 AM   #6
twindesert
LQ Newbie
 
Registered: Nov 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thank you very much!
I think I've managed to fix.
I didn't modify this line because using only this method works correctly. I modified the following:

private int[][] calcularRaices(int [][] m){

if (x != m.length - 1){
if (y == m[x].length - 1){
n = m[x][y];
r = 0;
raizCuadradaEntera(n);
m[x][y] = r;
x++;
y = 0;
calcularRaices(m);
}else{
n = m[x][y];
r = 0;
raizCuadradaEntera(n);
m[x][y] = r;
y++;
calcularRaices(m);
}
}
return m;
}



I have another problem .... : S

m [0] [0] = 1;
m [1] [0] = 10 // It would have to be 9 to be symmetrical, but applying raizCuadradaEntera -> 3, then [1][0] = [0][1] and would be true, but it comes out false.
m [2] [0] = 25;
m [3] [0] = 5;
m [0] [1] = 9 -> 3
m [1] [1] = 1;
m [2] [1] = 64;
m [3] [1] = 2;
m [0] [2] = 25;
m [1] [2] = 64;
m [2] [2] = 1;
m [3] [2] = 4;
m [0] [3] = 5;
m [1] [3] = 2;
m [2] [3] = 4;
m [3] [3] = 50;
 
Old 11-18-2011, 06:48 AM   #7
twindesert
LQ Newbie
 
Registered: Nov 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
My level of English is regular, forgive me if I write something wrong.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
i get the error "Exception in thread "main" java.lang.NoClassDefFoundError: Hello"... maheshbaria99 Programming 2 11-13-2011 02:13 PM
Exception in thread "main" java.lang.StackOverflowError+ at sun.awt.Win32GraphicsConf siegel Linux - Newbie 1 04-23-2009 08:04 AM
Exception in thread "main" java.lang.NoClassDefFoundError: org/aspectj/lang/Signature canonas Linux - Software 3 06-16-2008 04:00 AM
Exception in thread "main" java.lang.ClassFormatError: onmyown.Main (unrecognized cla zimboney Fedora 4 07-20-2007 01:00 PM
Java error "Exception in thread "main" java.lang.StackOverflowError" nro Programming 1 09-04-2004 03:47 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration