Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: cannot find symbol error with defining a string

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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?

    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;
    		}
     
     
    	}


  2. #2
    Junior Member
    Join Date
    Mar 2012
    Location
    St. Louis, MO
    Posts
    9
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Location
    St. Louis, MO
    Posts
    9
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Junior Member
    Join Date
    Jan 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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 >.<

  5. #5
    Junior Member
    Join Date
    Mar 2012
    Location
    St. Louis, MO
    Posts
    9
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  6. #6
    Junior Member
    Join Date
    Jan 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

Similar Threads

  1. [SOLVED] cannot find symbol error
    By Topflyt in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 5th, 2011, 08:57 AM
  2. error :cannot find symbol
    By iswan in forum AWT / Java Swing
    Replies: 1
    Last Post: October 1st, 2011, 08:26 AM
  3. Cannot find symbol error
    By AnuR in forum What's Wrong With My Code?
    Replies: 9
    Last Post: February 23rd, 2011, 02:50 PM
  4. cannot find symbol Error
    By bananasplitkids in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 9th, 2010, 02:36 AM
  5. Replies: 2
    Last Post: November 3rd, 2009, 06:28 AM