Results 1 to 8 of 8
  1. #1
    e.cummins95 is offline Member
    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0

    Default Palindrome Program

    Hey, I'm a learning Java programmer and this palindrome program isn't quite working. Thought I'd post on here for help. Oh, and I'm not allowed to use charAt() either. The program segment is below.. Thanks!

    Java Code:
    public static boolean palindrome(String temp)
    	{
    	    boolean isPalindrome = false;
    		String rev = "";
    		for (int x = temp.length()-1; x >= 0; x--)
    			rev += temp.substring(x, x+1);
    		if (rev == temp)
    		{
    			isPalindrome = true;
    			Util_edc.sopl("\nThe String \"" + temp + "\" is a palindrome.");
    		}//end if 1
    		else
    			if (rev != temp)
    			{
    				isPalindrome = false;
    				Util_edc.sopl("\nThe String \"" + temp + "\" is not a palindrome.");
    			}//end if 2
    	    return isPalindrome;
    
    	}// palindrome
    Last edited by pbrockway2; 03-08-2012 at 12:43 AM. Reason: code tags added

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Palindrome Program

    isn't quite working
    Perhaps you could describe what the problem is - what happens, and what you expected or intended would happen.

  3. #3
    Bestsanchez is offline Member
    Join Date
    Jan 2012
    Location
    The Coffee Pot
    Posts
    36
    Rep Power
    0

    Default Re: Palindrome Program

    Never use == for strings.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,528
    Blog Entries
    7
    Rep Power
    20

    Default Re: Palindrome Program

    Quote Originally Posted by e.cummins95 View Post
    Oh, and I'm not allowed to use charAt() either.
    Why not? Do they want crappy solutions or what?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    e.cummins95 is offline Member
    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0

    Default Re: Palindrome Program

    I don't know, my teacher wants us to use substring..

  6. #6
    e.cummins95 is offline Member
    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0

    Default Re: Palindrome Program

    No matter what is input, it always displays 'not a palindrome'

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Palindrome Program

    Quote Originally Posted by e.cummins95 View Post
    No matter what is input, it always displays 'not a palindrome'
    We (humans) say that two strings are "the same" when they consist of the same characters in the same order. But that's not what == does. == compares two variables or expressions to see if they have the same reference value ie if they reference exactly the same thing. The problem arises if we have (as we often have) two strings containing the same characters in the same order. They are still two strings and using == to compare variables that reference them will result in false, not true.

    This is what Bestsanchez was getting at.

    You can get the other meaning of "the same" by using the string method equals().

  8. #8
    e.cummins95 is offline Member
    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0

Similar Threads

  1. Palindrome Prime program
    By Zora in forum New To Java
    Replies: 7
    Last Post: 10-19-2011, 08:42 PM
  2. palindrome
    By rochellelising in forum New To Java
    Replies: 1
    Last Post: 08-24-2011, 01:03 PM
  3. Palindrome program
    By trinity in forum New To Java
    Replies: 4
    Last Post: 04-16-2011, 03:22 AM
  4. Palindrome program help
    By Nel in forum New To Java
    Replies: 1
    Last Post: 03-03-2011, 03:49 AM
  5. StringTokenizer in a Palindrome program
    By jeremyk in forum New To Java
    Replies: 10
    Last Post: 02-13-2010, 06:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •