Results 1 to 12 of 12
  1. #1
    Sheiib is offline Member
    Join Date
    Oct 2011
    Posts
    4
    Rep Power
    0

    Exclamation I need help with

    Hey, I'm doing this program (it's very simple) and I can't find the error in the code!
    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--));}
    }
    all it has to do, is see if the number has "a reflection" in the array.
    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 thread
    Last edited by Norm; 10-01-2011 at 08:33 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default 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.

  3. #3
    Sheiib is offline Member
    Join Date
    Oct 2011
    Posts
    4
    Rep Power
    0

    Default 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.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default 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?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default 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.

  6. #6
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default Re: I need help with

    As Norm said you need to give us the exact error message, and the code that's causing it.

  7. #7
    Sheiib is offline Member
    Join Date
    Oct 2011
    Posts
    4
    Rep Power
    0

    Default 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.

  8. #8
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default Re: I need help with

    Quote Originally Posted by Sheiib View Post
    PROGRAM
    Looks like you have never-ending recursion; your method is stuck calling itself over and over.

    This is happening because "if(s[a]==s[b])" never turns out to be true.
    Last edited by Solarsonic; 10-02-2011 at 02:49 AM.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default 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. #10
    Sheiib is offline Member
    Join Date
    Oct 2011
    Posts
    4
    Rep Power
    0

    Default 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.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default 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.

  12. #12
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default Re: I need help with

    Quote Originally Posted by Sheiib View Post
    No, it works when the number has a reflection.
    It doesn't work when the number hasn't a reflection.
    I explained your problem in my post above.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •