hey, how's everyone,
i need help with a java program, i'll appreciate any help.
i'm need to write a program in java that will test if the input is a Palindrome or not.
i found a couple of programs, but they use a (do-while) loop which i'm not allowed to use.
i have to write it using stacks and queues and using LinkedList with an iterator
here's my program:
/
import java.util.*;
public class Palindrome
{
public static void main(String[ ] args)
{
Scanner palindrome = new Scanner(System.in);
String input;
{
System.out.print("Enter Input: ");
input = palindrome.next();
if (isPalindrome(input))
System.out.println("That is a palindrome.");
else
System.out.println("That is not a palindrome.");
}
while (input.length() != 0);
}
public static boolean isPalindrome(String input)
{
Queue<Character> queue = new ArrayDeque<Character>();
Stack<Character> stack = new Stack<Character>(); Character letter;
int count = 0;
int i;
for (i = 0; i < input.length(); i++)
{
letter = input.charAt(i);
if (Character.isLetter(letter))
{
queue.add(letter);
stack.push(letter);
}
}
while (!queue.isEmpty())
{
if (queue.remove() != stack.pop())
count++;
}
not then that represents how many characters didn't match.
return (count == 0);
}
}
i'm told that my program goes into an infinite loop and that i shouldn't test the input to see if the characters are letters. javadoc comments say things that are not accurate.
The second program that uses LinkedList and Iterator:
/import java.util.*;
public class Palindrome
{
public static void main(String[ ] args)
{
Scanner palindrome = new Scanner(System.in);
String input;
List<Character> LinkedList = new ArrayList<Character>();
{
System.out.print("Enter Input: ");
input = palindrome.next();
if (isPalindrome(input))
System.out.println("That is a palindrome.");
else
System.out.println("That is not a palindrome.");
}
}
public static boolean isPalindrome(String input)
{
List<Character> LinkedList = new ArrayList<Character>();
int count = 0;
int i;
ListIterator begin = LinkedList.listIterator();
ListIterator end = LinkedList.listIterator();
for (i = 0; i < input.length(); i++)
{
LinkedList.add(input.charAt(i));
}
{
if (!(begin.next() == end.previous()))
count++;
}
return (count == 0);
}
}
here's the problems:
program will not run to completion, so it cannot produce correct output.
please help
thanx