954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Palindrome program using stack & queue

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 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);
 }
}
tnccjavaMatt
Newbie Poster
14 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

First you need to define what you consider a palindrome. Typically a palindrome is considered any String with the same characters (in both directions) regardless of case and regardless of spacing. So your isPalindrome method should

1. Remove all whitespace from your String (you can do this by using google) and typing "Java remove whitespace from string". I'd be careful to look at the solutions first, because I just took a look and some are much simpler than others.
2. Convert the entire thing to uppercase or lowercase (doesn't matter which) . . just use the toUpperCase method.
3. Scan each character at the front and at the back, making sure they match, until you reach the middle. If you can't figure out how to do this you can google for a palindrome program, there are plenty of examples.

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 

First you need to define what you consider a palindrome. Typically a palindrome is considered any String with the same characters (in both directions) regardless of case and regardless of spacing. So your isPalindrome method should

1. Remove all whitespace from your String (you can do this by using google) and typing "Java remove whitespace from string". I'd be careful to look at the solutions first, because I just took a look and some are much simpler than others. 2. Convert the entire thing to uppercase or lowercase (doesn't matter which) . . just use the toUpperCase method. 3. Scan each character at the front and at the back, making sure they match, until you reach the middle. If you can't figure out how to do this you can google for a palindrome program, there are plenty of examples.

Okay, I'm just about finished. The program now works on words and sentences, it properly ignores whitespace and punctuation, it pops output to JOptionPane. The only remaining problem didn't start until I switched from System.out.println to the JOptionPane. With the system.out.... when I pressed enter to exit, it exited, now with JOptionPane, enter doesn't exit. How do I fix?

import java.util.Scanner;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
import java.io.*;
import java.awt.*;
import javax.swing.*;

public class Palindrometest
{
 public static void main(String[] args) throws IOException
 {
  JFrame frame = new JFrame();
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
  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();
	//The next 3 lines removes upper case letters, whitespace and punctuation.
	line = line.toLowerCase();
	line = line.replaceAll(" ", "");
	line = line.replaceAll("\\W","");
	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);
 }
}
tnccjavaMatt
Newbie Poster
14 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

If you change your JOptionPanes from using "frame" as their first argument to using "null" as their first argument, your program will work. Alternatively, you could continue to use "frame" as the first argument to your JOptionPanes and add the line "frame.dispose()" after each JOptionPane is created and it will work.

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 

i need a complete java program that will accept a string and displays whether the string is palindrome or not, using only one stack implying one stack operation only. use push and pop operation and no string operation should be pperformed..

thanks.. ^^

dohn
Newbie Poster
1 post since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

^ Your program won't work without any String operations, unless your String is guaranteed to not have any spaces and the case is guaranteed to be the same throughout. You could potentially ignore spaces and convert the case on the fly, but then why not just do it to begin with? Didn't you listen to anything I said? Anyway, I'll trade you a program for a program:

I need a complete Java program that will translate any English sentence I enter into perfect Spanish. Once you give me this I'll write your program for you. Thanks.

P.S. This isn't your thread, if you had a legitimate question instead of that request for people to do your homework, I would have explained with far less sarcasm. But nobody cares, stop jacking threads.

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 

can you create a program that will take input from the user using a JOptionPane that will use a queue,stack,binary search tree,complete binary tree and a heapsort algorithm..haelp me please...my head is aching everytime I thinmk about this program...thank you...asap

hackenfracken
Newbie Poster
1 post since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

dirty little thread hijacker: we're not here to do your homework for you, and it is certainly NOT urgent.
If you have a headache frequently, go see a doctor.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

i am very much confused to implement a palindrome solving program in Microsoft Visual Studio.

kindly help to get out of the rid of this confusion....

irfan_iba
Newbie Poster
1 post since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

@irfan_iba: What language in VS?

You have hijacked someone else's thread in the wrong forum, demonstrated no effort at solving the issue on your own, and you haven't even indicated which language you're working with.

Quite a roll there.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

Hi;
can you help me?
I need (Palindrome using just stack and queue without using linkList)code

Thanks...

joooria
Newbie Poster
1 post since Apr 2011
Reputation Points: 7
Solved Threads: 0
 

Joooria...
have you even tried to read the answers the previous posters have given???
don't hijack threads, start new ones
next to that ...
have you tried anything yet, or are you just expecting us to write it for you?
write the code, see if it works or not, if not, try to solve the problems, if you can't on your own:
start a new thread, post your question together with any relevant code and error messages, and we 'll see how we can help you.

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You