Java中使用堆栈和队列应用问题的回文测试器

Java中使用堆栈和队列应用问题的回文测试器,java,palindrome,Java,Palindrome,我创建了一个应用程序来测试给定的输入字符串是否是回文。 但是这个程序没有检测到真正的回文,并且只在任何时候返回false。 我真的不明白为什么这个方法不起作用。 谁能给我一个建议吗? 谢谢 }这看起来不对 if (stack.peek() != queue.first()) 您正在尝试比较两个字符串。但是,代码仅检查字符串是否为同一对象 将字符串与进行比较=在Java中不起作用=检查比较的两个对象是否实际上是同一个对象。因此,您需要使用equals进行检查: if (!stack.peek()

我创建了一个应用程序来测试给定的输入字符串是否是回文。 但是这个程序没有检测到真正的回文,并且只在任何时候返回false。 我真的不明白为什么这个方法不起作用。 谁能给我一个建议吗? 谢谢

}这看起来不对

if (stack.peek() != queue.first())
您正在尝试比较两个字符串。但是,代码仅检查字符串是否为同一对象


将字符串与
进行比较=在Java中不起作用<代码>=检查比较的两个对象是否实际上是同一个对象。因此,您需要使用
equals
进行检查:

if (!stack.peek().equals(queue.first()))
除此之外,这似乎是一种非常复杂的检查字符串是否为回文的方法。基本上,回文是一个单词,它的前后读写相同。因此,只需反转输入字符串并将反转后的输入字符串与原始输入字符串进行比较,即可轻松解决此问题

public static void main(String[] args)
{
    //initiates a user input string 
    System.out.println("Enter a string to check: ");
    Scanner input = new Scanner(System.in);
    String palin = input.nextLine();

    //removes blank spaces in the input string
    palin = palin.replaceAll("\\s+","");

    // reverse the string
    String reverse = new StringBuilder(palin).reverse().toString();

    if (palin.equals(reverse)) { 
        System.out.println("Given string is a palindrome."); 
    }
    else {
        System.out.println("Given string is not a palindrome.");
    }
}

是的,我同意。这是任务的条件所以。。无论如何,谢谢你的帮助:)!
if (!stack.peek().equals(queue.first()))
public static void main(String[] args)
{
    //initiates a user input string 
    System.out.println("Enter a string to check: ");
    Scanner input = new Scanner(System.in);
    String palin = input.nextLine();

    //removes blank spaces in the input string
    palin = palin.replaceAll("\\s+","");

    // reverse the string
    String reverse = new StringBuilder(palin).reverse().toString();

    if (palin.equals(reverse)) { 
        System.out.println("Given string is a palindrome."); 
    }
    else {
        System.out.println("Given string is not a palindrome.");
    }
}