Ok, myself and a couple of guys from uni are working on a program which reads an input and outputs if the input is a palindrome or not. We're not allowed to use any stringBuffer methods, so this is where we're at with the code so far:
import javabook.*;
public class Palin
{
public static void main (String[] args)
{
MainWindow mainWindow = new MainWindow("Palindrome app");
InputBox inputBox = new InputBox(mainWindow);
OutputBox outputBox = new OutputBox(mainWindow);
mainWindow.setVisible(true);
outputBox.setVisible(true);
String s1="";
char temp=0;
char temp2=0;
int n=0;
boolean userHalt=false;
while(userHalt==false)
{
s1 = inputBox.getString("Please type in a word: " );
if(s1.equalsIgnoreCase("stop!"))
userHalt=true;
for(int i =0; i<s1.length(); i++)
{
n=s1.length();
temp=s1.charAt(0);
temp2=s1.charAt(n-1);
temp=temp++;
temp2=temp2--;
}
if(temp==temp2)
outputBox.printLine(s1 + " is a palindrome");
else
outputBox.printLine(s1 + " is not a palindrome");
}
}
}
The program works fine, until you mix cases. For example, with an input of "poop", it will return "poop is a palindrome". However, with an input of "Poop", it returns "Poop is not a palindrome". According to the Java API, we should be able to use toUpperCase, or toLowerCase to sort this, but I can't get it to work...
Any help or advice would be greatly appreciated
Bookmarks