944,022 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 7505
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 2nd, 2009
-1

Palindrome program using stack & queue

Expand Post »
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.


Java Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tnccjavaMatt is offline Offline
14 posts
since Oct 2009
Nov 3rd, 2009
0
Re: Palindrome program using stack & queue
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.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Nov 3rd, 2009
0
Re: Palindrome program using stack & queue
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?


Java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tnccjavaMatt is offline Offline
14 posts
since Oct 2009
Nov 3rd, 2009
0
Re: Palindrome program using stack & queue
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.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Nov 4th, 2009
1

help.!

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.. ^^
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dohn is offline Offline
1 posts
since Nov 2009
Nov 4th, 2009
0
Re: Palindrome program using stack & queue
^ 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.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Feb 26th, 2010
-1
Re: Palindrome program using stack & queue
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hackenfracken is offline Offline
1 posts
since Feb 2010
Feb 27th, 2010
1
Re: Palindrome program using stack & queue
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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Jan 24th, 2011
-1
Re: Palindrome program using stack & queue
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....
Reputation Points: 10
Solved Threads: 0
Newbie Poster
irfan_iba is offline Offline
1 posts
since Jan 2011
Jan 24th, 2011
0
Re: Palindrome program using stack & queue
@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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: j2me powerpoint application
Next Thread in Java Forum Timeline: What's wrog with my game's inventory code?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC