I have several questions I need help with.
I’ll add both my code and source code (I guess what assignment asks for clarification) in here.
Service Class
public class Palindrome
{
private String pal;
public Palindrome()
{
pal = " ";
}
public Palindrome(String newPal)
{
pal = newPal.toUpperCase();
}
public void setPal(String initPalin)
{
pal = initPalin.toUpperCase();
}
public String getPal()
{
return pal;
}
public boolean isPalindrome()
{
int left = 0;
int right = pal.length() -1;
while (pal.equals(toUpperCase))
{
if (pal.charAt(left) != pal.charAt(right));
{
return false;
}
left++;
right--;
}
return true;
}
public String toString()
{
return "Palindrome" + isPalindrome();
}
}
Client Class
import java.util.Scanner;
public class Palindromeclient
{
public static void main(String[]args)
{
String pal;
boolean isS = false;
Scanner scan = new Scanner(System.in);
System.out.println("Enter statement press[enter]:");
String userinput = scan.nextLine();
Palindrome statement = new Palindrome(pal);
isS = statement.isPalindrome();
if (isS)
System.out.println(userinput + "is a palindrome");
else
System.out.println(userinput + "is not a palindrome");
}
}
My coding is giving me a
Palindrome.java:34: error: cannot find symbol
while (pal.equals(toUpperCase))
^
symbol: variable toUpperCase
location: class Palindrome
1 error
I don’t get why though, can I simply add uppercase to the set or second constructor instead, which might be able to fix my service class.
That’s my question, number one
Answer to question 1:
toUpperCaseis a method of the String and should be invoked as one.Just like you did in the
setPalmethod.Answer to question 2:
The boolean in the main method is not needed, because you could ask your Palindrome object if it is a palidrome directly in de System.out.
Beware though, your program won’t work as you don’t pass the user input to the Palindrome Constructor.