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

Thread: Compare the last 4 strings in String s1 and String s2

  1. #1
    Junior Member javabro's Avatar
    Join Date
    Aug 2019
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Compare the last 4 strings in String s1 and String s2

    Hi!

    I'm quite new to Java and I've got this question that requires me to solve a basic algorithm.

    "Implement a method public static boolean compareLast4(String s1, String s2) that returns true if the final four-character substrings of s1 and s2 are equal. If the length of either string is less than 4, the method should return false.

    You may use the methods.substring(n,m) that yields the substring of s beginning at index and ending at index m-1."

    Here's my code:

    public class compareLast4
    {
        public static void main(String[] args){
            String s1 = "Elephant";
            String s2 = "Tiger";
            System.out.println(compareLast4(s1,s2));
        }
     
        public static boolean compareLast4(String s1, String s2){
            int p1 = s1.length() - 4;
            int p2 = s2.length() - 4;
            int t1 = 0;
            int t2 = 0;
     
            for (int i = p1; i < s1.length(); i++) {
                t1++;   
            }
            t1+=p1;
     
            for (int j = p2; j < s2.length(); j++) {
                t2++;
            }
            t2+=p2;
     
            if(s1.substring(p1, t1) == s2.substring(p2, t2)){
                return true;
            }
            else{
                return false;
            } 
        }
    }

    No matter what words I put in there, it's always false. The pointer is there to show the starting index of the string and it will count to the end. For example, "Elephant" starts at index 5, which is "h" until it reaches "t" and "tiger" starts at index 1 which is "i" until it reaches "r".

    Am I missing something?

    --- Update ---

    I've actually solved the problem, funnily enough.

    The following line has been added:
            if(s1.substring(p1, t1).length() == s2.substring(p2, t2).length()){
                return true; 
            }
    Last edited by javabro; August 26th, 2019 at 03:09 PM.

  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: Compare the last 4 strings in String s1 and String s2

      if(s1.substring(p1, t1) == s2.substring(p2, t2)){

    You should use the equals method for comparing the contents of String objects, not the == operator.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member javabro's Avatar
    Join Date
    Aug 2019
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Compare the last 4 strings in String s1 and String s2

    I've tried the equals method and it doesn't work.

    The one I've created works because I'm comparing numbers using the .length().

  4. #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: Compare the last 4 strings in String s1 and String s2

    I've tried the equals method and it doesn't work.
    Sorry, the equals method is the only reliable way to compare the contents of String objects.
    Can you make a small, simple program that shows the problem and paste it here?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member javabro's Avatar
    Join Date
    Aug 2019
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Compare the last 4 strings in String s1 and String s2

    If you're referring to compare two strings with the same last 4 letters, then yes, equals method does work.

    public class compareLast4
    {
        public static void main(String[] args){
            String s1 = "bathroom";
            String s2 = "bedroom";
            System.out.println(compareLast4(s1,s2));
        }
     
        public static boolean compareLast4(String s1, String s2){
            int p1 = s1.length() - 4; 
            int p2 = s2.length() - 4; 
            int t1 = 0;
            int t2 = 0;
     
            if(s1.length() >=4 && s2.length() >= 4){
     
                for (int i = p1; i < s1.length(); i++) {
                    t1++;   
                }
     
                t1+=p1;
     
                for (int j = p2; j < s2.length(); j++) {
                    t2++;
                }
     
                t2+=p2;
     
                if(s1.substring(p1, t1).equals(s2.substring(p2, t2))){
                    return true; 
                }
                else{
                    return false;
                } 
            }
            else{
                return false;
            }
        }
    }

    But the question says something like this:
    "returns true if the final four-character substrings of s1 and s2 are equal"

    And I think it means comparing to the number of substrings?

  6. #6
    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: Compare the last 4 strings in String s1 and String s2

    What is the purpose of the two for loops?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member javabro's Avatar
    Join Date
    Aug 2019
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Compare the last 4 strings in String s1 and String s2

    The first for loop is to get the total length of s1 and the second for loop is to get the total length of s2. I'm doing these to get the last index of the strings.

    I realised I can actually do something like this as well:
          if(s1.substring(p1, s1.length()).equals(s2.substring(p2, s2.length()))){
                    return true; 
                }

    Which means, there's no need to create two for loops.

  8. #8
    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: Compare the last 4 strings in String s1 and String s2

    get the last index of the strings.
    It is not needed.
    Look at the API doc for the String class's substring method. There is a better way.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member javabro's Avatar
    Join Date
    Aug 2019
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Compare the last 4 strings in String s1 and String s2

    Do you mean something like this?
                    if(s1.substring(p1).equals(s2.substring(p2))){
                        return true; 
                    }

    Originally, I want to do something like this but I wasn't sure about my interpretation with the question. When I read the API doc, it says that it can only compare if the characters are the same. So I guess, what it meant by equal, is that, if they're the same characters then it's true.

  10. #10
    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: Compare the last 4 strings in String s1 and String s2

    Yes that looks like what I was talking about. There is no need for the ending index if you want all of the String starting from the first index.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. how do I use my previous string compare to current string?
    By sydethsam in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 3rd, 2014, 06:13 AM
  2. [SOLVED] How To Compare String input to char Array....
    By Praetorian in forum Java Theory & Questions
    Replies: 13
    Last Post: February 12th, 2014, 03:34 AM
  3. compare two strings to see if they end with the same sub-string
    By tuathan in forum Loops & Control Statements
    Replies: 2
    Last Post: July 7th, 2012, 06:56 AM
  4. [SOLVED] How to compare a parameter value to a string literal inside EL?
    By Lord Voldemort in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: July 28th, 2011, 06:58 PM
  5. String + Compare // Might be too easy for ya
    By Jangan in forum Java Theory & Questions
    Replies: 1
    Last Post: October 18th, 2009, 05:40 PM

Tags for this Thread