Needs to ignore case....
I have been working on a program for my class and I have it working...well everything except that it doesn't ignore the case of what is typed in. I know I should use .equalsIgnoreCase and that I probably need to have subString in there instead of the charAt so that it cna compare them correctly, but I'm confused on what way to type this so that it compiles. Any help would be great. Thanks!
Here's the code that I've written so far...and in case you need to know it's a program to see if what was typed in is a palindrome.
if (pal.length() <= 1)
returntrue;
elseif (pal.charAt(0) != pal.charAt(pal.length() - 1))
returnfalse;
else
return recursivePalindrome(pal.substring(1, pal.length() - 1));
I don't get an error message with the way it's coded right now. If I were to run it, and I type in "Racecar"...which is a palindrome it will say that its not because one r is capitalized. So I need to make the second if statement ignore the case of the typed in message.
but here's the code:
import javax.swing.JOptionPane;
import javax.swing.JFrame;
import java.awt.Graphics;
public class PalindromeClient extends JFrame
{
private Palindrome palind;
boolean started = false;
public PalindromeClient( )
{
palind = new Palindrome( );
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
setSize( 570, 400 );
setVisible( true );
}
public Palindrome getPalindrome( )
{
return palind;
}
public void setStarted( boolean b )
{
started = b;
}
public boolean recursivePalindrome( String pal )
{
// ***** Student writes the body of this method *****
// Using recursion, determine if a String representing
// a word or a sentence is a palindrome
// If it is, return true, otherwise return false
// We call the method animate inside the body of this method
// The call to animate is already coded below
animate( pal );
//
// Student code starts here
//
if (pal.length() <= 1)
return true;
else if (pal.charAt(0) != pal.charAt(pal.length() - 1))
return false;
else
return recursivePalindrome(pal.substring(1, pal.length() - 1));
//
// End of student code
//
}
public void animate( String pal )
{
palind.updatePalindrome( pal );
palind.setStarted( true );
started = true;
repaint( );
try
{
Thread.sleep( 1000 );// wait for the animation to finish
}
catch ( Exception e )
{
}
}
public void paint( Graphics g )
{
if ( started )
{
super.paint( g );
palind.draw( g );
}
}
public String getPalString( )
{
boolean goodInput = false;
String palString = "";
// palString is limited to 26 characters
while( !goodInput )
{
try
{
palString = JOptionPane.showInputDialog( null, "Enter a word or phrase between 1 and 26 characters long" );
if ( palString != null )
{
if ( palString.length( ) <= 26 && palString.length( ) >= 1 )
goodInput = true;
}
else
{
System.exit( 0 );
}
}
catch( Exception e )
{ }
}
return palString;
}
public static void main( String [] args )
{
PalindromeClient app = new PalindromeClient( );
// ask user for input
boolean result = false;
// the program will loop until the user clicks on "Cancel"
while( true )
{
String appPal = app.getPalString( );
( app.getPalindrome( ) ).setPalString( appPal );
app.setStarted( true );
// start
result = app.recursivePalindrome( ( app.getPalindrome( ) ).getPalString( ) );
// finish last step in animation
( app.getPalindrome( ) ).setResult( result );
app.repaint( );
System.out.println( "The correct result is " + ( app.getPalindrome( ) ).getExactResult( ) );
System.out.println( "Your result is " + result );
System.out.println( "Done\n" );
// done
JOptionPane.showMessageDialog( null, "Done" );
}
}
}
or:
public boolean recursivePalindrome( String pal )
{
// ***** Student writes the body of this method *****
// Using recursion, determine if a String representing
// a word or a sentence is a palindrome
// If it is, return true, otherwise return false
// We call the method animate inside the body of this method
// The call to animate is already coded below
animate( pal );
//
// Student code starts here
//
String p = pal.toUpperCase();
if (pal.length() <= 1)
return true;
else if (p.charAt(0) != pal.charAt(p.length() - 1))
return false;
else
return recursivePalindrome(p.substring(1, pal.length() - 1));
//
// End of student code
//
}
> public boolean recursivePalindrome( String pal )
> {
>String p = pal.toUpperCase();
>...
>return recursivePalindrome(p.substring(1, pal.length() - 1));
> }
One minor suggestion: this will create an additional String object.
Of course, the actual correct thing for the OP to do is to
not construct any String (or substring) at all.
:)
Edit: I just checked java 1.5's source code for String.toUpperCase().
It's actually clever enough to return the existing String if it is
already all upper case. Neat!