Results 1 to 12 of 12
Thread: I need help with
- 10-01-2011, 07:22 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 4
- Rep Power
- 0
I need help with
Hey, I'm doing this program (it's very simple) and I can't find the error in the code!
all it has to do, is see if the number has "a reflection" in the array.Java Code:import java.util.Random; public class NúmerosEnteros{ private int []s; public NúmerosEnteros (int n, Random azar){ s=new int [n]; int a=0; for (int k=0; k<n; k++){ a=azar.nextInt(10); s[k]=a;}} public boolean tieneReflejo(){ return (tr(0, s.length-1));} public boolean tr(int a, int b){ if (b<=a) return false; else if(s[a]==s[b]) return true; else return (tr(a++,b--));} }
it means that if the array is empty or has just one element, it doesn't have a reflection.
in other case:
you all know the array saves numbers like this
d0 d1 d2 d3... d(n-1) dn
so, if d0=dn, or d1=d(n-1), or d2=d(n-2), etc., then it has a reflection.
else, it doesn't.
can someone tell me where the error is? I can't find it.
EDIT: Moved post to this threadLast edited by Norm; 10-01-2011 at 08:33 PM.
- 10-01-2011, 08:31 PM #2
Re: I need help with java.lang.stackoverflowerror
Why do you think there is an error?
Can you post the output from when the program executes and add some comments saying what the output should be. Show the input and the output.
Create a complete test program that will execute and show the problem. I don't see a main method so what you posted will not execute.
- 10-01-2011, 08:42 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 4
- Rep Power
- 0
Re: I need help with
Because when I run the tester it says that's the error, java.lang.StackOverflowError
the tester is this:
import java.util.Random;
public class Tester
{
public static void main(String[] args) {
NúmerosEnteros ne;
Random aleatorio = new Random();
ne= new NúmerosEnteros(7, aleatorio);
String mostrar=ne.toString();
System.out.println(" ");
System.out.println("Original: "+mostrar);
System.out.println(" ");
System.out.println("Tiene reflejo: ");
if (ne.tieneReflejo()) System.out.print("Sí");
else System.out.print("No");
}
that's it. it compiles, but then when I run it, if the number doesn't have a reflection it doesnt work.
- 10-01-2011, 08:47 PM #4
Re: I need help with
Your error message does give you a line number. Did you look at that line to see why the code is calling itself?
- 10-01-2011, 08:50 PM #5
Re: I need help with
Try debugging your code by adding printlns to show the values of the variables as they change and to show the execution flow.
You need to see why your logic doesn't stop calling the tr() method.
- 10-02-2011, 02:31 AM #6
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
Re: I need help with
As Norm said you need to give us the exact error message, and the code that's causing it.
- 10-02-2011, 02:40 AM #7
Member
- Join Date
- Oct 2011
- Posts
- 4
- Rep Power
- 0
Re: I need help with
PROGRAM
import java.util.Random;
public class NúmerosEnteros{
private int []s;
public NúmerosEnteros (int n, Random azar){
s=new int [n]; int a=0;
for (int k=0; k<n; k++){
a=azar.nextInt(10);
s[k]=a;}}
public boolean tieneReflejo(){
return (tr(0, s.length-1));}
public boolean tr(int a, int b){
if (b<=a) return false;
else
if(s[a]==s[b]) return true;
else return (tr(a++,b--));}
}
TESTER
import java.util.Random;
public class Tester
{
public static void main(String[] args) {
NúmerosEnteros ne;
Random aleatorio = new Random();
ne= new NúmerosEnteros(7, aleatorio);
String mostrar=ne.toString();
System.out.println(" ");
System.out.println("Original: "+mostrar);
System.out.println(" ");
System.out.println("Tiene reflejo: ");
if (ne.tieneReflejo()) System.out.print("Sí");
else System.out.print("No");
}
ERROR MESSAGE
java.lang.StackOverflowError:
null
the part in red is the part that produces the error.
- 10-02-2011, 02:47 AM #8
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
- 10-02-2011, 02:49 AM #9
Re: I need help with
Try debugging your code by adding printlns to show the values of the variables as they change and to show the execution flow.
You need to see why your logic doesn't stop calling the tr() method.
- 10-02-2011, 03:02 AM #10
Member
- Join Date
- Oct 2011
- Posts
- 4
- Rep Power
- 0
Re: I need help with
No, it works when the number has a reflection.
It doesn't work when the number hasn't a reflection.
- 10-02-2011, 03:03 AM #11
Re: I need help with
Have you tried debugging it?
Try debugging your code by adding printlns to show the values of the variables as they change and to show the execution flow.
You need to see why your logic doesn't stop calling the tr() method.
- 10-02-2011, 03:03 AM #12
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks