| | |
Palindrome program using stack & queue
![]() |
•
•
Join Date: Oct 2009
Posts: 8
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; 21 Days Ago at 10:58 pm.
•
•
Join Date: Sep 2008
Posts: 1,563
Reputation:
Solved Threads: 196
0
#2 21 Days Ago
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: 8
Reputation:
Solved Threads: 0
0
#3 21 Days Ago
•
•
•
•
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,563
Reputation:
Solved Threads: 196
0
#4 20 Days Ago
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,563
Reputation:
Solved Threads: 196
0
#6 20 Days Ago
^ 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; 20 Days Ago 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??
| Thread Tools | Search this Thread |
actuate add android api applet application applications array arrays automation balls bank binary bluetooth business chat class clear client code codesnippet collections component database defaultmethod development dice digit dragging ebook eclipse equation error event formatingtextintooltipjava fractal functiontesting game givemetehcodez graphics gui health hql html hyper ide idea image infinite int integer invokingapacheantprogrammatically j2me java javame javaprojects jni jpanel julia linux list main map method methods mobile myregfun mysql netbeans nonstatic openjavafx parameter pearl php problem program project recursion repositories scanner scrollbar server set sms sort sorting spamblocker sql sqlserver state storm string sun superclass swing swt thread threads tree windows






