| | |
Palindrome program using stack & queue
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2009
Posts: 14
Reputation:
Solved Threads: 0
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.
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)
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); } }
Last edited by tnccjavaMatt; Nov 2nd, 2009 at 10:58 pm.
•
•
Join Date: Sep 2008
Posts: 1,658
Reputation:
Solved Threads: 206
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.
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.
•
•
Join Date: Oct 2009
Posts: 14
Reputation:
Solved Threads: 0
0
#3 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.
Java Syntax (Toggle Plain Text)
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); } }
•
•
Join Date: Sep 2008
Posts: 1,658
Reputation:
Solved Threads: 206
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.
•
•
Join Date: Sep 2008
Posts: 1,658
Reputation:
Solved Threads: 206
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.
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.
![]() |
Similar Threads
- Palindrome Program (Java)
- simple palindrome program (C++)
- Help on stack , queue, palindrome program... (C++)
- How to write a program using a stack (C++)
Other Threads in the Java Forum
- Previous Thread: Java Software Help
- Next Thread: hey guys...can someone help me??
Views: 1060 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for Java
-xlint android animated api apple applet application arguments array arrays automation binary blackberry block bluetooth chat class classes client code component database detection developmenthelp draw eclipse encode error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer iphone j2me j2seprojects java javac javaprojects jmf jni jpanel julia lego linux list loop loops mac map method methods mobile netbeans newbie number object online oracle os page print problem program programming project recursion scanner screen server set singleton size sms socket sort sql string swing template test textfields threads time title transfer tree tutorial-sample update windows working






