【问题标题】:Palindrome Checker using Stack in Java (Always returning false)在 Java 中使用 Stack 的回文检查器(总是返回 false)
【发布时间】:2013-10-22 08:09:23
【问题描述】:

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

这是我的回文客户端文件:(我的 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

【问题讨论】:

  • 当你想“今天不会有==equals的问题!”的那一刻。
  • 这不是解决方案,但是如果您添加到 (!fwd.isEmpty() && !rev.isEmpty()) && palindrome ,您可以节省一点运行时间。同样在回文=假之后;你可以调用 break,因为你已经知道它不是回文。
  • @Maroun 还在熟悉 java 语言。不会再犯那个愚蠢的错误,现在我明白了其中的区别!
  • @MagdaleneB。没关系 :) 每个人都会犯错。

标签: java stack


【解决方案1】:

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

如果您检测到它不是回文,您还应该 break 您的 while 循环,否则“测试”将返回 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

【讨论】:

  • 只是添加信息,不需要while里面的for循环。
  • 谢谢你的工作!仍然以这些有趣的小测试项目的形式熟悉 java。 :)
  • @RamonBoza 谢谢你,我明白你的意思了,我现在把它拿出来了。
  • 啊哈。我懂了。我最初尝试制作一个 char 堆栈,但是当我得到一个编译错误时:'code'Stack fwd = new Stack();我没有进一步尝试。现在我知道如何定义字符堆栈了,谢谢!
  • @MagdaleneB。你也可以保持你的字符串堆栈是正确的。但是检查一个单词是否为回文的主要思想是逐个字符地检查。您可以直接将字符本身放在堆栈中,而不是将字符放在 String 对象中,但是您的版本也可以 =)
猜你喜欢
  • 2017-08-02
  • 1970-01-01
  • 1970-01-01
  • 2015-10-20
  • 2019-10-06
  • 1970-01-01
  • 2013-08-13
  • 2021-06-24
  • 2011-05-07
相关资源
最近更新 更多