Palindrome program using stack & queue

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2009
Posts: 14
Reputation: tnccjavaMatt is an unknown quantity at this point 
Solved Threads: 0
tnccjavaMatt tnccjavaMatt is offline Offline
Newbie Poster

Palindrome program using stack & queue

 
0
  #1
Nov 2nd, 2009
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.


  1. import java.util.Scanner;
  2. import java.util.LinkedList;
  3. import java.util.Queue;
  4. import java.util.Stack;
  5. import java.io.*;
  6.  
  7. public class Palindrometest
  8. {
  9. public static void main(String[] args) throws IOException
  10. {
  11. Scanner stdin = new Scanner(System.in);
  12. //Scanner stdin = new Scanner(new File("input.txt"));
  13. String line;
  14.  
  15. do
  16. {
  17. System.out.print("Your expression (or return to end): ");
  18. line = stdin.nextLine();
  19. if (is_palindrome(line))
  20. System.out.println("that is a palindrome");
  21. //JOptionPane.showMessageDialog(frame, "That is a palindrome");
  22. else
  23. //JOptionPane.showMessageDialog(frame, "That is NOT a palinfrome");
  24. System.out.println("that is NOT a palindrome");
  25. }
  26. while (line.length() != 0);
  27. }
  28.  
  29. public static boolean is_palindrome(String input)
  30. {
  31. Queue<Character> q = new LinkedList<Character>();
  32. Stack<Character> s = new Stack<Character>();
  33. Character letter;
  34. int mismatches = 0;
  35.  
  36. for (int i = 0; i < input.length(); i++)
  37. {
  38. letter = input.charAt(i);
  39. if (Character.isLetter(letter))
  40. {
  41. q.add(letter);
  42. s.push(letter);
  43. }
  44. }
  45. while (!q.isEmpty())
  46. {
  47. if (q.remove() != s.pop())
  48. mismatches++;
  49. }
  50. return (mismatches == 0);
  51. }
  52. }
Last edited by tnccjavaMatt; Nov 2nd, 2009 at 10:58 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,620
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 205
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso
 
0
  #2
Nov 3rd, 2009
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.
Out.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 14
Reputation: tnccjavaMatt is an unknown quantity at this point 
Solved Threads: 0
tnccjavaMatt tnccjavaMatt is offline Offline
Newbie Poster
 
0
  #3
Nov 3rd, 2009
Originally Posted by BestJewSinceJC View Post
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?


  1. import java.util.Scanner;
  2. import java.util.LinkedList;
  3. import java.util.Queue;
  4. import java.util.Stack;
  5. import java.io.*;
  6. import java.awt.*;
  7. import javax.swing.*;
  8.  
  9. public class Palindrometest
  10. {
  11. public static void main(String[] args) throws IOException
  12. {
  13. JFrame frame = new JFrame();
  14. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  15.  
  16. Scanner stdin = new Scanner(System.in);
  17. //Scanner stdin = new Scanner(new File("input.txt"));
  18. String line;
  19.  
  20. do
  21. {
  22. System.out.print("Your expression (or return to end): ");
  23. line = stdin.nextLine();
  24. //The next 3 lines removes upper case letters, whitespace and punctuation.
  25. line = line.toLowerCase();
  26. line = line.replaceAll(" ", "");
  27. line = line.replaceAll("\\W","");
  28. if (is_palindrome(line))
  29. //System.out.println("that is a palindrome");
  30. JOptionPane.showMessageDialog(frame, "That is a palindrome");
  31. else
  32. JOptionPane.showMessageDialog(frame, "That is NOT a palinfrome");
  33. //System.out.println("that is NOT a palindrome");
  34. }
  35. while (line.length() != 0);
  36. }
  37.  
  38. public static boolean is_palindrome(String input)
  39. {
  40. Queue<Character> q = new LinkedList<Character>();
  41. Stack<Character> s = new Stack<Character>();
  42. Character letter;
  43. int mismatches = 0;
  44.  
  45. for (int i = 0; i < input.length(); i++)
  46. {
  47. letter = input.charAt(i);
  48. if (Character.isLetter(letter))
  49. {
  50. q.add(letter);
  51. s.push(letter);
  52. }
  53. }
  54. while (!q.isEmpty())
  55. {
  56. if (q.remove() != s.pop())
  57. mismatches++;
  58. }
  59. return (mismatches == 0);
  60. }
  61. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,620
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 205
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso
 
0
  #4
Nov 3rd, 2009
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.
Out.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 1
Reputation: dohn is an unknown quantity at this point 
Solved Threads: 0
dohn dohn is offline Offline
Newbie Poster

help.!

 
0
  #5
Nov 4th, 2009
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.. ^^
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,620
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 205
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso
 
0
  #6
Nov 4th, 2009
^ 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.
Last edited by BestJewSinceJC; Nov 4th, 2009 at 4:51 am.
Out.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC