Results 1 to 2 of 2
  1. #1
    Nel
    Nel is offline Member
    Join Date
    Mar 2011
    Posts
    1
    Rep Power
    0

    Smile 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);
    			
    			
    			
    	}
    	
    	
    	
    	
    	
    	
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Java Code:
    compareStr =  compareStr.replaceAll("\\D", "");
    System.out.println(">" + compareStr + "<"); //add this line to get your answer
    By the way
    Java Code:
    while(left<right && left!=right)
    the second condition is pointless if the first condition is true then the second MUST be true.

Similar Threads

  1. Palindrome question
    By luke in forum New To Java
    Replies: 20
    Last Post: 10-02-2010, 02:17 AM
  2. Palindrome
    By pinkdreammsss in forum Java Applets
    Replies: 8
    Last Post: 05-04-2010, 04:59 PM
  3. StringTokenizer in a Palindrome program
    By jeremyk in forum New To Java
    Replies: 10
    Last Post: 02-13-2010, 07:35 PM
  4. HELP...Palindrome
    By d7o0om in forum New To Java
    Replies: 12
    Last Post: 11-13-2009, 04:32 AM
  5. Palindrome problem
    By Mika in forum New To Java
    Replies: 4
    Last Post: 02-18-2009, 03:46 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •