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 4 of 4

Thread: Return Value Cannot Be Found

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Return Value Cannot Be Found

    I need to make a code testing if an inputted name is a palindrome. I wrote a method that is supposed to return a String (ex. "[name] is a palindrome.") to the main method. However, the compiler "cannot find symbol" of my final return value ("return palindrome;"). Could anyone please help? Thanks!
    This is only the method I'm having trouble with; I can paste the main method, too, if someone wants me too, but it's not much.
        public static String palindrome (String name) {
            int length = name.length();
            int i = 0;
            while (i < length) {
                char first = name.charAt(i);
                String firstStr = first + "";
                char last = name.charAt(length-1-i);
                String lastStr = last + "";
                if (firstStr.equals (lastStr)) {
                    i++;
                    if (i == length - 1) {
                        String palindrome = name + " is a palindrome.";
                    }
                } else {
                    String palindrome = name + " is not a palindrome.";
                }
            }
            return palindrome;
        }

    Also- I'm new and don't know if this is the right forum. If it's not, please tell me which one is the correct forum to post this to. Thanks!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Return Value Cannot Be Found

    You have defined the String palindrome locally inside of a pair of {}s. Its definition is NOT known outside of the enclosing {}s. It is out of scope there.
    You need to define it at the same level of {}s where you want to use it, at the method level.
    When you define it you will have to assign it a null value because it is in a method where variables do not get default values.

  3. The Following User Says Thank You to Norm For This Useful Post:

    misscoollike (March 12th, 2012)

  4. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Return Value Cannot Be Found

    Thank you, this worked and your explanation was clear! New problem: when I actually run the program, I think it's getting stuck in a loop. You don't have to, but is there any glaringly obvious problem with this method?

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Return Value Cannot Be Found

    To help figure out what is happening, add some printlns to the code to print out the values of the variables that are used to control the loop as the loop executes. The print out should show you why the execution is getting stuck in the loop.

Similar Threads

  1. No suitable driver found
    By Josheh in forum JDBC & Databases
    Replies: 2
    Last Post: October 16th, 2011, 01:12 AM
  2. Return Object does not return the expected output
    By Nour Damer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 13th, 2011, 07:24 AM
  3. [SOLVED] JDBC_Servlet: Data not found
    By mithcool in forum Java Servlet
    Replies: 4
    Last Post: June 24th, 2011, 11:36 AM
  4. application id not found
    By pradeepsetty in forum Java IDEs
    Replies: 3
    Last Post: June 2nd, 2010, 02:37 AM
  5. Jsp source not found
    By jadeite100 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 15th, 2010, 01:00 AM

Tags for this Thread