I have two issues that I need to solve on this program that are giving me some troubles. I have scanned google and my text book and so far coming up empty.
Problem #1 - My program works for palindrome words and 2 word palindromes like "race car". But I need it to work for any length of a sentence, like "Borrow or rob?" or "Egad! A base life defiles a bad age". So somehow I need to add code to the isPalindrome method.
Problem #2 - Currently I am using system.out.println (mainly to test program), but the final product must use JOptionPane. The code is currently commented out since it errors out otherwise. Currently I do not have the normal JFrame code that initializes the frame variable, but when I insert it, I get <identifier> expected error messages.
import java.util.Scanner;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
import java.io.*;
public class Palindrometest
{
public static void main(String[] args) throws IOException
{
Scanner stdin = new Scanner(System.in);
//Scanner stdin = new Scanner(new File("input.txt"));
String line;
do
{
System.out.print("Your expression (or return to end): ");
line = stdin.nextLine();
if (is_palindrome(line))
System.out.println("that is a palindrome");
//JOptionPane.showMessageDialog(frame, "That is a palindrome");
else
//JOptionPane.showMessageDialog(frame, "That is NOT a palinfrome");
System.out.println("that is NOT a palindrome");
}
while (line.length() != 0);
}
public static boolean is_palindrome(String input)
{
Queue<Character> q = new LinkedList<Character>();
Stack<Character> s = new Stack<Character>();
Character letter;
int mismatches = 0;
for (int i = 0; i < input.length(); i++)
{
letter = input.charAt(i);
if (Character.isLetter(letter))
{
q.add(letter);
s.push(letter);
}
}
while (!q.isEmpty())
{
if (q.remove() != s.pop())
mismatches++;
}
return (mismatches == 0);
}
}