1.11M Members

Palindrome program using stack & queue

 
-1
 

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);
 }
}
 
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.

 
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.

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);
 }
}
 
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.

 
1
 

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.. ^^

 
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.

 
-1
 

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

 
1
 

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.

 
-1
 

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....

 
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.

 
-1
 

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

Thanks...

 
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.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article