#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Palindrome tester


    I need to figure out a way to test palindromes.



    Code:
    import javax.swing.JOptionPane;
    
    public class Palindromes 
    {
    
    	private static String convertString(String s)
    	{
    		String converted = " ";
    		char current;
    		
    
    		for (int i=0; i<s.length(); i++)
    		{
    			
    			current = s.charAt(i);
    			
    			if (Character.isLetterOrDigit(current))
    			{
    				if(Character.isUpperCase(current))
    				
    					current = Character.toLowerCase(current);
    					converted += current;
    					
    			
    			}	
    			
    		}
    		
    			return converted;
    			
    	}
    }
    
    public static void main (String[] args)
    {
    	String str, result, in;
    	int left, right;
    	int another;
    	do
    	{
    		in = JOptionPane.showInputDialog ("Enter a number:");
    		str = convertString(in);
    		
    		left = 0;
    		right = str.length() - 1;
    		
    		while (str.charAt(left) == str.charAt(right) && left < right)
    		{
    			left++;
    			right--;
    		}
    		
    		result = "\"" + in + "\"";
    		if (left < right)
    			result += "is not a palindrome.";
    		else 
    			result += "is a palindrome.";
    		JOptionPane.showMessageDialog(null, result);
    		
    		another = JOptionPane.showConfirmDialog(null,"Test an other?"
    	);
    		}
    	While (another == JOptionPane.YES_OPTION);
    	
    	}
    }
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,023
    Rep Power
    1285
    What's your question?

IMN logo majestic logo threadwatch logo seochat tools logo