Palindrome Checker在Java中使用Stack(总是返回false)

时间:2013-10-22 08:09:24

标签: java stack

我正在尝试用Java创建自己的Stack类。我不确定我的代码在哪里出错了,但是如果我在程序控制台中输入一个实际的回文,它总是返回false。我的问题在哪里发生?

这是我的palindrome客户端文件:(我的stack.java类可以在同一个程序文件夹中访问。)

public static void main(String args[]) {

//declare both stacks
Stack<String> fwd = new Stack<String>();
Stack<String> rev = new Stack<String>();

//instance variables
String st1;
boolean palindrome = true;

Scanner in = new Scanner(System.in);

System.out.println("Enter a String and I will check if it is a palindrome: \n");

st1 = in.nextLine();

// Read the data in forward order into the Stack
for (int i =0; i < st1.length(); i++) {
  fwd.push(Character.toString(st1.charAt(i)));
}
// Read the same data in reverse order into another Stack
for (int j = st1.length()-1; j >= 0; j--) {
  rev.push(Character.toString(st1.charAt(j)));
}
System.out.println("The string you entered was: ");
System.out.println(fwd.display());
System.out.println(rev.display());
System.out.println();
System.out.println("Checking to see if " + st1 + " is a palindrome.");
System.out.println("/**********************************************");

//check fwd and rev against each other
while (!fwd.isEmpty() && !rev.isEmpty()) {  // make sure stack in not empty
  for (int i = 0; i < st1.length(); i++) {  // go through each element in the stack

     if (fwd.pop() == rev.pop()) //check if fwd pop and rev pop are the same
        palindrome = true;
     else
        palindrome = false;

  }//end for lop
}// end while loop

System.out.println(palindrome);


}// end main

}// end Palindrome

这是我的Stack.java文件:

public class Stack<E> implements StackInterface<E> {

   //variables
   private ArrayList<E> data;
   private E element;


   //constructor
   public Stack() {

      data = new ArrayList<E>();
   }


   //stack methods
   public void push(E element) { //push new element into the stack

      data.add(element);
   }

   public E pop() { //pop the element from the top

      if (data.isEmpty()) //if stack is empty, throw exception
         throw new EmptyStackException("The stack is empty.");
      else //else, remove and return the element that is on top of the stack
         return data.remove(data.size()-1); 
   }

   public E peek() { //peek at the element on top of the stack without removing it

      if (data.isEmpty()) //if stack is empty, throw exception
         throw new EmptyStackException("The stack is empty.");
      else //else, return the element that is on top of the class
         return data.get(data.size()-1);   
   }

   public String display() { //display the elements in the stack in the form of a String

      if (data.isEmpty()) //if stack is empty, throw exception
         throw new EmptyStackException("The stack is empty");
      else //else, return elements as a String
         return data.toString();
   }

   public boolean isEmpty() { //check to see if the stack is empty

      if (data.size() == 0)
         return true;
      else
         return false;

   }

   public int size() { //retrurn the number of elements in the stack

      return data.size();

   }

}// end Stack class

1 个答案:

答案 0 :(得分:5)

if (fwd.pop() == rev.pop())应为if (fwd.pop().equals(rev.pop())),因为您要比较两个字符串的内容。

如果你发现它不是回文,你也应该break你的while循环,否则“test”会返回true

if (fwd.pop().equals(rev.pop())) //check if fwd pop and rev pop are the same
                palindrome = true;
             else {
                palindrome = false;
                break;
                 }
}

修改:

如上所述,while循环中的for循环实际上是没用的。

另一种选择是创建两个Characters堆栈,现在您可以使用==来比较它们:

Stack<Character> fwd = new Stack<>();
Stack<Character> rev = new Stack<>();

/**/
for (int i =0; i < st1.length(); i++) {
    fwd.push(st1.charAt(i));
}
for (int j = st1.length()-1; j >= 0; j--) {
    rev.push(st1.charAt(j));
}
while (!fwd.isEmpty() && !rev.isEmpty()) {  // make sure stack in not empty
        if (fwd.pop()==rev.pop()) //check if fwd pop and rev pop are the same
           palindrome = true;
        else {
           palindrome = false;
           break; //here break the while loop it's not a palindrom
        }
}// end while loop