cannot find symbol error with defining a string
So this code is supposed to take an entered string, turn that string backward, then compare the original and backward string to see if they match and return true if they are palindromes. Before turning the text backward, I'm trying to convert it to lowercase, however I'm getting a "cannot find symbol" error on the line where I take the parameter passed to the method, convert it to lowercase, and store it in a new variable.
I have no idea what's causing this, from what I know about java(which isn't much), the "cannot find symbol" error while defining a variable within a method is often caused when the method or class cannot access whatever value is assigned to the new variable, however it seems to me like method isPal should have no problem being given the variable 'str'. Please, can someone tell me where I'm going wrong?
Code :
public class Lab14TEXT05st
{
public static void main (String args[])
{
System.out.println("\nLab14TEST05\n");
boolean finished = false;
do
{
System.out.print("Enter a string ===>> ");
String str = Expo.enterString();
System.out.println();
System.out.println("Entered String: " + str);
System.out.println("Palindrome: " + Palindrome.isPal(str)); <---This line should be passing string str to 's' in the isPal method
System.out.println("Almost Palindrome: " + Palindrome.almostPal(str)); // used only for the 100 and 110 point versions
System.out.println("Least Palindrome: " + Palindrome.leastPal(str)); // used only for the 110 point versions
System.out.println();
System.out.print("Do you wish to repeat this program [Y/N]? ===>> ");
char repeat = Expo.enterChar();
finished = (repeat != 'Y' && repeat != 'y');
System.out.println();
}
while (!finished);
}
}
class Palindrome
{
public static boolean isPal(String s)
/*
* Precondition: s is an arbitrary String.
* Postcondition: The value of true is returned if s is a Palindrome, false otherwise.
*/
{
String s2 = s.toLowerCase; <--The error message points to this line
String s3 = "";
int n = s.length() - 1;
for (int k = n; k >= 0; k--){
s3 += s2.charAt(k);
}
if (s2.equals(s3)){
return true;
}
}
Re: cannot find symbol error with defining a string
Is your public static void main(String[] args) mixed up. I am not sure yours is right unless my teacher is telling me only one way of putting that.
Re: cannot find symbol error with defining a string
I don't know much either but I'm trying to learn and be good at it.
Re: cannot find symbol error with defining a string
writing (String args[]) should also be correct, the program compiled and ran before I edited the method, and I didn't edit the String args[] part. Thanks for the ideas though, I'm learning too and this error always seems to happen to me and I'm never able to figure out why >.<
Re: cannot find symbol error with defining a string
Java is hard but I can't get enough of it lol. And anytime sorry that I'm not much help Trying to branch out and meet new coders like myself.
Re: cannot find symbol error with defining a string
Cool, yeah I'm trying to learn java and C++ as well, but I'm taking java in school so when I'm having issues programming at home I can't ask a teacher :-/ . I just figured out what the issue was also, for anyone who might visit this thread wanting an answer:
toLowercase is itself a method in the String class, so it requires parenthesis after it, like this: s.toLowercase();
Also, since the isPal method is a boolean method, I had to edit my return statement to make both a true and a false outcome, instead of just the true that I had in my if statement, like this:
if (s2.equals(s3))
return true;
else
return false;
Now it compiles and runs like it's supposed to :-j