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

Thread: Compare strings

  1. #1
    Junior Member
    Join Date
    Apr 2022
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Compare strings

    public class CompareString {
        public static void main(String[] args) {
     
     
            StringBuilder s1 = new StringBuilder("axx#bb#c");
            StringBuilder s2 = new StringBuilder("axbd#c#c");
     
            if (compareStrings(s1, s2))
                System.out.print("True");
            else
                System.out.print("False");
        }
     
        public static boolean compareStrings(StringBuilder s1, StringBuilder s2) {
            int countS1 = -1;
            int countS2 = -1;
     
     
            for (int i = 0; i < s1.length(); i++) {
     
     
                if (s1.charAt(i) == '#' && countS1 != -1)
                    countS1 -= 1;
     
     
                else if (s1.charAt(i) != '#') {
                    s1.setCharAt(countS1 + 1, s1.charAt(i));
                    countS1 += 1;
                }
            }
     
     
            for (int i = 0; i < s2.length(); i++) {
     
     
                if (s2.charAt(i) == '#' && countS2 != -1)
                    countS2 -= 1;
     
                else if (s2.charAt(i) != '#') {
                    s2.setCharAt(countS2 + 1, s2.charAt(i));
     
                    countS2 += 1;
                }
            }
            if (countS2 != countS1)
                return false;
     
     
            else if (countS1 == -1 && countS2 == -1)
                return true;
     
            else {
                for (int i = 0; i <= countS2; i++) {
                    if (s1.charAt(i) != s2.charAt(i))
                        return false;
                }
                return true;
     
     
            }
        }
    }

    The code works, but the teacher insisted that instead of the Boolean type there should be an int type, and that it would return not true or false, but 1 or 0

    public static int compareStrings(String s1, String s2)

    pls help )
    Last edited by amnez1ya; April 10th, 2022 at 04:21 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Compare strings

    Did you look at the String class to see if it has any methods that do what you want?

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

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

    amnez1ya (April 7th, 2022)

  4. #3
    Junior Member
    Join Date
    Apr 2022
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Compare strings

    I didn't quite understand your question...
    I can't figure out how to change this code so that it returns 1 and 0.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Compare strings

    how to change this code so that it returns 1 and 0
    What are the conditions for the code to return a 0?
    What are the conditions for the code to return a 1?
    Given answers to those two questions, work on the code to do it.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Apr 2022
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Compare strings

    Thanks for the thought, I'll take a look at it

Similar Threads

  1. [SOLVED] Compare the last 4 strings in String s1 and String s2
    By javabro in forum What's Wrong With My Code?
    Replies: 9
    Last Post: August 27th, 2019, 05:41 AM
  2. Why doesn't my if condition work when I compare two strings?
    By elbiociq in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 12th, 2014, 02:07 AM
  3. Sorting lowercase strings before uppercase strings
    By keepStriving in forum Java Theory & Questions
    Replies: 4
    Last Post: March 26th, 2014, 03:33 PM
  4. 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
  5. [SOLVED] Assign a loop to variable to compare strings
    By norske_lab in forum Loops & Control Statements
    Replies: 3
    Last Post: March 6th, 2012, 02:46 PM