Results 1 to 10 of 10
  1. #1
    reverton is offline Member
    Join Date
    Apr 2013
    Posts
    11
    Rep Power
    0

    Default Compare letters of 2 strings

    I want to compare letters of 2 same size strings. If they are the same return different return 1 and if they are the same return zero. I just don't understand why i++ is not working:


    Java Code:
     for(int i=0; i<sequence1.length();i++){
            	
            	if(sequence1.charAt(i)==sequence2.charAt(i)){
            		
            		return 0;	
            	}
            	else{
            		return 1; 	
            	}

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: Compare letters of 2 strings

    It "is" working. The problem is that you are returning either a 0 or a 1 first time through. You only want to return 0 if all the characters are equal. But you can return a 1 the first time the characters are not equal.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

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

    Default Re: Compare letters of 2 strings

    What makes you suppose that the increment operator (++) is broken?

    Consider posting runnable code and saying what you expect or intend to happen.

  4. #4
    reverton is offline Member
    Join Date
    Apr 2013
    Posts
    11
    Rep Power
    0

    Default Re: Compare letters of 2 strings

    Thanks for your reply.

    It is saying dead code.

    I want to know how many different words random strings have. I started writing the code above, but there is already this problem with ++.

  5. #5
    reverton is offline Member
    Join Date
    Apr 2013
    Posts
    11
    Rep Power
    0

    Default Re: Compare letters of 2 strings

    P.S.: The letters compared must be in same position. If string 1 = abcd and string 2 = abbd I want the result of 1 because there is 1 letter in same position that is different(third letter in both strings).

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: Compare letters of 2 strings

    There is no problem with the autoincrement operator. It is your coding logic which is incorrect.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    reverton is offline Member
    Join Date
    Apr 2013
    Posts
    11
    Rep Power
    0

    Default Re: Compare letters of 2 strings

    Yes, you are right.

    Now I tried:

    Java Code:
    int counter = 0;
        	
            for(int i=0; i<sequence1.length();i++){
            	
            	if(sequence1.charAt(i)!=sequence2.charAt(i)){
            		
            		counter++;	
            	}
            	return counter;
    where sequence1 and sequence 2 are 2 strings of same size. And counter would be the number of different letters at same position in both strings. But I am still doing something wrong, because ++ in the loop still warns as dead code. Can you advise me what could be my mystake? Thanks.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: Compare letters of 2 strings

    The dead code is being caused by the fact that you are returning during the first iteration of the loop. So i++ never has a chance to increment. What you need to do is the following:

    Java Code:
    for(int i=0; i<sequence1.length();i++){
         if(sequence1.charAt(i)!=sequence2.charAt(i)){
            return 1;  // characters not equal so strings not equal
         }
    }
    return 0;  // all characters equal so strings are equal
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    haowilliam is offline Member
    Join Date
    Apr 2013
    Posts
    1
    Rep Power
    0

    Default Re: Compare letters of 2 strings

    length is a field ,not a method ,so you should code as "length" rather than "length()";

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

    Default Re: Compare letters of 2 strings

    Quote Originally Posted by haowilliam View Post
    length is a field ,not a method ,so you should code as "length" rather than "length()";
    For arrays it's a field; for Strings (and other object types) it's a method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 4
    Last Post: 04-27-2013, 11:04 AM
  2. Replies: 2
    Last Post: 12-04-2011, 08:21 AM
  3. Compare two strings
    By Proshitness in forum JDBC
    Replies: 1
    Last Post: 10-12-2011, 09:06 PM
  4. changing letters in strings
    By dienesh77 in forum New To Java
    Replies: 8
    Last Post: 02-17-2011, 01:56 PM
  5. text Strings to produce letters
    By dc2acgsr99 in forum New To Java
    Replies: 7
    Last Post: 01-29-2008, 08:08 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
  •