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

Thread: Simple but Strange situation in my Java Code.

  1. #1
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Simple but Strange situation in my Java Code.

    I have a simple code to test palindrome. I have the word "civic" and I got this code:

    public String isPalindrome(String word) {
    		StringBuilder builder = new StringBuilder(word);
    		String reversed = builder.reverse().toString();
    		String s = new String();
    		if(reversed.equalsIgnoreCase(word)){
    		 s = "Word " + parameters +  " evitative is a palindrome!";
    		}
    		return  s;
       }

    to my surprise, the if() condition is false even though the word is a palindrome. Any help please.

    Thanks


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,043
    My Mood
    Mellow
    Thanks
    184
    Thanked 648 Times in 635 Posts

    Default Re: Simple but Strange situation in my Java Code.

    No errors? What is 'parameters'?

  3. #3
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Simple but Strange situation in my Java Code.

    Sorry, the parameters is a typo but why is the if() condition false? you mean you got it to be true?

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,043
    My Mood
    Mellow
    Thanks
    184
    Thanked 648 Times in 635 Posts

    Default Re: Simple but Strange situation in my Java Code.

    I changed 'parameters' to 'word', initialized 's' as "Not a palindrome", made it static, and called the method you posted with my changes from a main() method with 2 words, one that is and another that isn't a palindrome, and it provided the correct result.

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    help_desk (July 25th, 2014)

  6. #5
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Simple but Strange situation in my Java Code.

    Hi, still not getting the if() condition to be true.

    public String isPalindrome(String word) {
    		StringBuilder builder = new StringBuilder(word);
    		String reversed = builder.reverse().toString();
    		String s = new String();
    		if(reversed.equalsIgnoreCase(word)){
    		 s = "Word " + word +  " is a palindrome!";
    		}
    		return  s;
       }

    the mystery is in this lines of code:

    if(reversed.equalsIgnoreCase(word)){
    		 s = "Word " + word +  " is a palindrome!";
    		}

    I tested with the word :"evitative" the if() condition is false even though reversed and word are the same.
    Last edited by help_desk; July 28th, 2014 at 11:01 AM. Reason: format line

  7. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,297
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Simple but Strange situation in my Java Code.

    I tested with the word :"evitative" the if() condition is false even though reversed and word are the same.
    How do you know it evaluates to false? The code you posted does not contain any way to test this...it is always best to post an SSCCE that shows all the code necessary to reproduce the problem

  8. #7
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Simple but Strange situation in my Java Code.

    Quote Originally Posted by copeg View Post
    How do you know it evaluates to false? The code you posted does not contain any way to test this...it is always best to post an SSCCE that shows all the code necessary to reproduce the problem
    I stated clearly that the statement in the if() condition was NEVER executed. That means the if() condition translated/interpreted to false. Otherwise, it would have gone into the next line of
    s = "Word " + word +  " is a palindrome!";

    it didn't that means the if() condition was false.

  9. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,297
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Simple but Strange situation in my Java Code.

    I stated clearly that the statement in the if() condition was NEVER executed.
    You did not state clearly how you know this. Did you add println's in there to see? Are you relying on the string returned from the method? Are you relying on a debugger? Whichever way, if you read post #4:

    it provided the correct result
    Suggesting the problem may be in the manner in which you are testing/calling the code, without which we can only guess what the problem is.

  10. #9
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Simple but Strange situation in my Java Code.

    Quote Originally Posted by copeg View Post
    You did not state clearly how you know this. Did you add println's in there to see? Are you relying on the string returned from the method? Are you relying on a debugger? Whichever way, if you read post #4:



    Suggesting the problem may be in the manner in which you are testing/calling the code, without which we can only guess what the problem is.

    I did add some System.out.println() for the reversed and word strings. the reversed string was "evitative" while the word string was also "evitative" but when I checked their equality using the if(reversed.equalsIsIgnore(word)) it was false.. add a System.out.println() round reversed.equalsIgnoreCase(word) and it gives FALSE. why this is so is so baffling. This is what I need help on: to know why this is so.

    --- Update ---

    Here is how I have tested it:


     
    public String isPalindrome(String word) {
     
                    String s = new String("word is NOT a palindrome");
     
    		StringBuilder builder = new StringBuilder(word);
    		String reversed = builder.reverse().toString();
    		System.out.println(reversed + "\n");
    		System.out.println(word + "\n");
    		System.out.println(reversed.equalsIgnoreCase(word) + "\n");
     
    		if(reversed.equalsIgnoreCase(word)){
    		 s = "Word " + word +  " is a palindrome!";
    		}
    		return  s;
       }

  11. #10
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,297
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Simple but Strange situation in my Java Code.

    And how are you calling the method? When you run that code, what does it print? More information is often better (removes the guesswork) - here's an example of an SSCCE, using your code above plus a main method to call isPalindrome:

    public class Test {
     
    	public static void main(String[] args){
    		System.out.println(isPalindrome("evitative"));
    	}
     
    	public static String isPalindrome(String word) {
     
    		String s = new String("word is NOT a palindrome");
     
    		StringBuilder builder = new StringBuilder(word);
    		String reversed = builder.reverse().toString();
    		System.out.println(reversed + "\n");
    		System.out.println(word + "\n");
    		System.out.println(reversed.equalsIgnoreCase(word) + "\n");
     
    		if(reversed.equalsIgnoreCase(word)){
    			s = "Word " + word +  " is a palindrome!";
    		}
    		return  s;
    	}
    }

    Prints:

    evitative
     
    evitative
     
    true
     
    Word evitative is a palindrome!

  12. #11
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Simple but Strange situation in my Java Code.

    Everything displays as yours except the if() condition being false. This is what I want to know. I have called it just like you called. Pass in a string and then reverse it in the method and compare the reverse and the string passed in .

  13. #12
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,297
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Simple but Strange situation in my Java Code.

    Quote Originally Posted by help_desk View Post
    Everything displays as yours except the if() condition being false.
    Post exactly what you ran, and the result. If you ran the code I posted above, the output should be identical. Without knowing what you ran, as well as the context, we are only left to guess.

  14. #13
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Simple but Strange situation in my Java Code.

    Quote Originally Posted by copeg View Post
    Post exactly what you ran, and the result. If you ran the code I posted above, the output should be identical. Without knowing what you ran, as well as the context, we are only left to guess.
    What I have done is a bit lengthy. it's a project where a user has to enter the command to be executed at the command line and then the instruction is read and interpreted by getting the operator first and then based on the operator the right command is executed on the operands. So, when a user enters isPalindrome civic on the command line. that string is split into "isPalindrome" and "civic". the programme checks the first string which is "isPalindrome" and based on this, calls the isPalindrome method. Now, posting that here might be abit stressful as it is is a full project and that is NOT allowed here. What I have done is debug the method and check that the second part of the string "civic" is passed into the ispalindrome method. IF there is a way to post a full project here, kindly let me know.

    thanks

  15. #14
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,297
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Simple but Strange situation in my Java Code.

    IF there is a way to post a full project here, kindly let me know.
    I asked for an SSCCE that demonstrates the problem - see my signature for a full description of what that is, posting the full project will result in a lot of code unrelated to the problem (and code many don't want to wade through). Creating an SSCCE is not just for our benefit but for yours, because it is an essential technique in debugging (and often one can diagnose the problem simply in the process of creating one)

    This being said, without seeing how you are calling the method (with what input) we can't really help you because - as the SSCCE I posted above demonstrates - the problem does not appear to be contained within the code posted.

  16. #15
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,043
    My Mood
    Mellow
    Thanks
    184
    Thanked 648 Times in 635 Posts

    Default Re: Simple but Strange situation in my Java Code.

    IF there is a way to post a full project here, kindly let me know.
    I'm not sure what the "full project" is, but of course there's a way to test the method. Here's an example:
    public class TestClass
    {
        public static void main( String[] args )
        {
            System.out.println( isPalindrome( "civic" ) );
            System.out.println( isPalindrome( "your name here" ) );
        }
     
     
        public static String isPalindrome(String word)
        {
     
            String s = word + " is NOT a palindrome";
     
            StringBuilder builder = new StringBuilder(word);
            String reversed = builder.reverse().toString();
            System.out.println(reversed + "\n");
            System.out.println(word + "\n");
            System.out.println(reversed.equalsIgnoreCase(word) + "\n");
     
            if(reversed.equalsIgnoreCase(word)){
                s = word +  " is a palindrome!";
            }
            return  s;
     
        }
    }

  17. #16
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Simple but Strange situation in my Java Code.

    I have tested it and even explained to coopeg countless times that there is virtually nothing else except the mystery remains. I passed in a string into the isPalindrome() method and wrote a couple of system.out.println() to see what was happening. To my surprise, the if() condition interpretes to false. That is what I can't figure out. Why the guy keeps insisting I have to post him more data left me wondering IF he understood my plight.

  18. #17
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,043
    My Mood
    Mellow
    Thanks
    184
    Thanked 648 Times in 635 Posts

    Default Re: Simple but Strange situation in my Java Code.

    I know that I don't. Post #5 indicates the if() condition is never true, but the test I posted clearly indicates that the if() condition is true when the word passed to the method is a palindrome.

    I'm assuming at this point there is no plight. The method works in that it correctly identifies when the word passed to it is a palindrome and when it is not. If it doesn't work as you'd like, you might try commenting the method to describe what it's supposed to do and then commenting the code within the method to describe how each step is written to accomplish the method's purpose and post that with any sample runs you'd like to show that demonstrate how it's not working correctly.

  19. #18
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Simple but Strange situation in my Java Code.

    steps:
    1. user enters the following on request:

    "isPalindrome evitative"

    2. this string is passed into the commands interprete method

    3. In the interprete method, the string is split into two parts: "isPalindrome" and "evitative"
    a. the first part "isPalindrome" is passed into a find(operand) method to check that the
    command has any such operand.
    b.IF that is true, then it goes through a switch loop to call the appropriate method for the operand, in this case, the IsPalindrome object is called.
    c. and in the IsPalindrome class is the isPalindrome method that finally does the computation.

    public static void main(String[] args) {
    		System.out.println("Enter a word please : \n");
    		/*
    		* I tested with the word evitative
    		*/
    		Scanner st = new Scanner(System.in);
    		String word = st.nextLine();
    		String result = commands.interprete(word);
    		System.out.println(result);
    		}
     
     
    INTERPRETE METHODS AND HELPER METHODS
     
     
    public String getOperands(String[] lineOperands){
    	StringBuilder operands = new StringBuilder("");
    	String [] lineParts = new String[lineOperands.length-1];
     
    	for(int i = 1, j = 0; i<=lineParts.length; ++i) 
    		lineParts[j++] = lineOperands[i];
     
    		for(String operand : lineParts){
    			operands.append(operand);
    			operands.append(" ");
    		}
    		return operands.toString();
     
    	}
     
     
    	public boolean find(String operator){
    			boolean flag = false;
    			for(Command command : commandArray){
    				if(command.getStr().equals(operator)){
    				  flag = true;
    				  break;
    				}
     
    			}
     
    			return flag;
    		}
     
     
    public String interprete(String s){
     
    		String[] lineParts = s.split("\\s+");
    		String operator = lineParts[0];
    		String operatorCase = operator.toLowerCase(); 
    		if(find(operator.toLowerCase())){ 
    			switch(operatorCase){
     
     
     
     
    			case "capitals" : 	s = getOperands(lineParts);
    								Capitals capital = new Capitals();
    							  	s = capital.makeCapital(s);
    							  	break;
     
    			case "ispalindrome": s = getOperands(lineParts);
    								 IsPalindrome palindrome = new IsPalindrome();
    								 s = palindrome.isPalindrome(s);
    								 break;
     
     
     
    			}
    		} else System.out.println("Command " + operator + " is unknown!");
     
    		return s;
     
    	}
     
     
     
    	isPalindrome(String word){
    	String s = new String("Word is NOT palindrome");
    	StringBuilder builder = new StringBuilder(word);
    			String reversed = builder.reverse().toString();
     
    			System.out.println(reversed + "\n");
    			System.out.println(word + "\n");
    			System.out.println(reversed.equalsIgnoreCase(word) + "\n");
     
    			if(word.equals(reversed)){
    			 s = "Word " + parameters +  " is a palindrome!";
    			}
    			return  s;
     
    	}
    Last edited by help_desk; July 28th, 2014 at 04:57 PM. Reason: format line

  20. #19
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,297
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Simple but Strange situation in my Java Code.

    Both Greg and I posted SSCCE's that demonstrate the method (as posted) works as expected with the test input. So my advice would be to backup and have a hard look at the input for this method...is the input to the isPalindrome method what you expect (you can test this with a few println's)? Are you sure the data that is parsed/returned by getOperands is what you expect/correct? (hint, the answer to these questions is no)

  21. #20
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Simple but Strange situation in my Java Code.

    Is there anyway I could send you the full project? I have checked everything my brain can check and still don't find it. I believe the problem could be from the getOperands method before passing it into the isPalindrome method but when I debugged the IsPalindrome, the parameter was correct.

  22. #21
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,297
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Simple but Strange situation in my Java Code.

    I don't need the full project - the issue is contained within the code you posted (but not an easy thing to spot). Like I said, inspect that you are passing what you expect the isPalindrome method - for instance, if you enter "isPalindrome civic" does the parameter to isPalindrome equal 'civic'? If not, back up in your code to see how this may have occurred

Similar Threads

  1. Errors in my Simple Java code
    By help_desk in forum Java Theory & Questions
    Replies: 5
    Last Post: July 7th, 2014, 12:06 PM
  2. Simple animation. Strange behavior
    By silvercats in forum AWT / Java Swing
    Replies: 2
    Last Post: June 17th, 2014, 04:37 PM
  3. Java strange thread behavior
    By Bingo90 in forum Threads
    Replies: 7
    Last Post: October 19th, 2013, 10:53 AM
  4. classes design for Multithreading situation
    By harry7ster in forum Threads
    Replies: 2
    Last Post: October 9th, 2013, 08:12 AM
  5. how do i loop this simple java code?
    By legend101z in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 27th, 2013, 05:46 PM

Tags for this Thread