Results 1 to 6 of 6
  1. #1
    Yakg is offline Member
    Join Date
    Dec 2010
    Posts
    59
    Rep Power
    0

    Default can't see where it went wrong..

    Hi, I'm writing a method that checks a palindrome in a stack.
    At the moment it returns true even when the values are not equal.
    I would appreciate some help, as I didn't locate the problem.

    Java Code:
    public static boolean PalindromeWithStack (Stack s, int numOfElements){
    int counter = 0,first,second;
    Stack s2=new Stack ();
    
    if (numOfElements ==0) return true; if (numOfElements ==1) return false;
    while (counter<numOfElements/2){
    s2.push(s.pop());
    counter++;
    } //devide the stacks evenly.
    
    if (numOfElements %2 != 0) //if odd number of elements
    s.pop();
    
    while (counter > 0){
    first = s.pop(); second = s2.pop();
    if (first!=second){        //after condition
    return false;  //ends here
    }
    counter--;   
        }
    return true;
         }
        }
    Thank you.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by Yakg View Post
    At the moment it returns true even when the values are not equal.
    Really? I got false when it is not a palindrome. What data are you testing your code with?

    BTW your code is a bit messy. A better way is: if the current value is equal to the top element on the stack > pop else push. When you are done if the stack is empty then it was a palindrome else it wasn't. Mind you there are 2 special conditions: the first value and the middle value in an odd number of values.

  3. #3
    Yakg is offline Member
    Join Date
    Dec 2010
    Posts
    59
    Rep Power
    0

    Default I'm trying to

    put ints in the palindrome, in this case 3 and 1.
    As for the parameters it takes the stack with the two items and the number 2 as the second parameter.

    I can't use other methods besides: push and pop

  4. #4
    Yakg is offline Member
    Join Date
    Dec 2010
    Posts
    59
    Rep Power
    0

    Default Yeah something doesn't make sense..

    In the tester I'm inserting these lines:

    Stack s1 = new Stack();
    s1.push(1);
    s1.push(3);

    and then pass it to the method as: (s1,2). The first is the stack and the second is the number of elements..

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Yakg View Post
    In the tester I'm inserting these lines:

    Stack s1 = new Stack();
    s1.push(1);
    s1.push(3);

    and then pass it to the method as: (s1,2). The first is the stack and the second is the number of elements..
    Did you try to use Stack<Integer> instead of just a 'raw' Stack class? As your code is now it doesn't even compile.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Yakg is offline Member
    Join Date
    Dec 2010
    Posts
    59
    Rep Power
    0

    Default we are very restricted

    this is sounds weird, this is the tester class I've been given so I can't change it.
    The only thing is changeable is the palindrome class.
    It is compiling well when I try it, just something with the condition maybe is wrong.

    We are not allowed to use any other ways to solve it..

    thanks for helping me getting through this, hopefully one day I will be able to help in this topic as well.

Similar Threads

  1. Replies: 4
    Last Post: 06-11-2013, 01:37 AM
  2. What am I doing wrong?
    By yankeefan9874 in forum New To Java
    Replies: 1
    Last Post: 04-16-2010, 08:54 AM
  3. does anyone know what is wrong with this?
    By blueduiker in forum New To Java
    Replies: 5
    Last Post: 03-22-2010, 10:25 AM
  4. what am i doing wrong here?
    By GPB in forum New To Java
    Replies: 3
    Last Post: 03-21-2010, 04:04 PM
  5. What am I doing wrong??
    By NoNickName in forum New To Java
    Replies: 3
    Last Post: 04-23-2009, 11:04 PM

Posting Permissions

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