0

Hello i am new to programming and am learning Data Structures on Java, i am making a simple Palindrome program that works fine, but the only think i am stuck with is that when i enter the String to exit from the program it continues to Loop. I am trying to have it continue the loop when the user enters anything but "!"

package Palindrome;
import java.util.Stack;
import java.util.Scanner;
public class Palindrome {
static String input;
static String reverseInput = "";
public Palindrome() {
    // TODO Auto-generated constructor stub
}

public static void main(String[] args) {

    Scanner keyboard = new Scanner(System.in);
    String check = null;
    System.out.println("Enter a string that you want to check (or enter ! to exit): ");
            input = keyboard.nextLine();
    do
    {       
            input = keyboard.nextLine();
             Stack<Character> stack = new Stack<Character>();
             for (int i = 0; i < input.length(); i++) {
                    stack.push(input.charAt(i));
             }

             while (!stack.isEmpty()) {
                    reverseInput += stack.pop();
             }
             isPalindrome(reverseInput);


        System.out.println("Enter a string that you want to check (or enter ! to exit): ");
            input = keyboard.nextLine();

    } while (input != "!");
}

public static boolean isPalindrome(String aString){
    if (input.equals(reverseInput))
        System.out.println("Yes! that is a palindrome.");
    else
        System.out.println("No! that isn't a palindrome.");
    return false;
}

}

0 Answers0