Results 1 to 2 of 2
Thread: Palindrome program help
- 03-03-2011, 03:37 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 1
- Rep Power
- 0
Palindrome program help
Hey guys, so I'm brand new to java and this forum. This is my first post. I seem to be having difficulty with this program. I know I have a logic error in my code, but I can't seem to locate it. The program is supposed to take any series of characters, including numbers and test if it is a palindrome. If any symbols !@#$%^&* are entered, they are to be ignored. (So 1@23!43#2-1 is a palindrome.) It works fine for numbers, but it says every string input is a palindrome. I havnt gotten far into the book, so my tools are limited to basic string manipulations and basic loop structures. Please help by giving only suggestions or hints; no code as I am trying to learn =) Thanks! (Also, how do I put code into the post without Copy/Paste and manual formatting? I couldn't find/understand in the FAQ.)
Java Code:import java.util.Scanner; public class Palindrome { public static void main(String[]args) { Scanner keyboard = new Scanner(System.in); String userStr, newStr, compareStr; int right, left; newStr = ""; compareStr = ""; System.out.print("Please enter any sequences of characters: "); userStr = keyboard.nextLine(); newStr = userStr.toLowerCase(); compareStr = newStr.trim(); compareStr = compareStr.replaceAll("\\D", ""); right = compareStr.length() - 1; left = 0; while(left<right && left!=right) { if(compareStr.charAt(left)==(compareStr.charAt(right))) { right--; left++; } else { System.out.print(userStr + " is not a Palindrome."); System.exit(0); } } System.out.print(userStr + " is a Palindrome."); System.exit(0); } }
- 03-03-2011, 03:49 AM #2
Similar Threads
-
Palindrome question
By luke in forum New To JavaReplies: 20Last Post: 10-02-2010, 01:17 AM -
Palindrome
By pinkdreammsss in forum Java AppletsReplies: 8Last Post: 05-04-2010, 03:59 PM -
StringTokenizer in a Palindrome program
By jeremyk in forum New To JavaReplies: 10Last Post: 02-13-2010, 06:35 PM -
HELP...Palindrome
By d7o0om in forum New To JavaReplies: 12Last Post: 11-13-2009, 03:32 AM -
Palindrome problem
By Mika in forum New To JavaReplies: 4Last Post: 02-18-2009, 02:46 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks