I'll take a look at it. Can you post your new code?
Printable View
I'll take a look at it. Can you post your new code?
Here is your modified code. I modified it so it does what its suppose to. By now you must have understood the cases I mentioned earlier. I did this really quick and it should work but if you find a case that doesnt let me know so I will look at it more carefully. :) Although, I am sure this will work.
Quote:
/*
Palindrome v1.0
#############Palindrome Revised###########
*/
import javabook.*;
public class Palin
{
public static void main (String[] args)
{
MainWindow mainWindow = new MainWindow("Palindromes");
InputBox inputBox = new InputBox(mainWindow);
OutputBox outputBox = new OutputBox(mainWindow);
outputBox.setVisible(true);
outputBox.printLine("---------Type stop! to quit program-------------");
String s1="";
String s2="";
char tempval=0;
char tempval2=0;
int n=0;
boolean userHalt=false;
while(userHalt==false)
{
s1 = inputBox.getString("Please type in a word: " );
s2 = s1.toUpperCase();
if(s1.equals("exit"))
{
userHalt=true;
break;
}
n=s2.length();
boolean pal= true;
for(int i=0; i<n/2; i++)
{
//n=s2.length(); /-> No Need for this (CHANGE MADE HERE)
tempval=s2.charAt(i);
tempval2=s2.charAt(n-i-1);
//CHANGES MADE HERE
if(tempval!=tempval2)
{
pal=false;
break;
}
}
//CHANGES MADE HERE
if(pal==true){
outputBox.printLine(s1 + " is a palindrome");
}
else
outputBox.printLine(s1 + " is not a palindrome");
}
}
}
Thanks a lot :)
That works perfectly
One little thing though is that you should change the text displayed in the outputbox to "type exit" to stop the program.
You are welcome. :)
Oh yes, I used "exit" to quit instead of stop!, but didnt change it in the display :lol: