/*help me to correct this error*/
import javax.swing.JOptionPane;
public class Assignment1
{
public static void main(String args[])
{
String input = JOptionPane.showInputDialog("Enter a string");
if (istPalindrome(input))
{
JOptionPane.showMessageDialog(null,input+"is a palindrome");
}
else{
JOptionPane.showMessageDialog(null ,input +"is not a palindrome");
}
public static boolean istPalindrome(String a)/*error is here*/
{
char[] charArray = word.toCharArray();
int i1 = 0;
int i2 = word.length() - 1;
while (i2 > i1) {
if (charArray[i1] != charArray[i2]) {
return false;
}
++i1;
--i2;
}
return true;
}
}
}
2 Answers
As stated by ratchet freak, you need to move the function out of main. The function itself belongs to the Assignment1 class and as such needs to be:
import javax.swing.JOptionPane;
public class Assignment1
{
public static void main(String args[])
{
String input = JOptionPane.showInputDialog("Enter a string");
if (istPalindrome(input))
{
JOptionPane.showMessageDialog(null,input+"is a palindrome");
}
else{
JOptionPane.showMessageDialog(null ,input +"is not a palindrome");
}
}
public static boolean istPalindrome(String a)/*error is here*/
{
char[] charArray = a.toCharArray();
int i1 = 0;
int i2 = a.length() - 1;
while (i2 > i1) {
if (charArray[i1] != charArray[i2]) {
return false;
}
++i1;
--i2;
}
return true;
}
}
I've also changed the variable "word" over to "a" (for the statements word.toCharArray() and word.length()) due to the fact that there is no "word" variable in scope within that function.
istPalindromeis defined inside themainfunction)