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

Thread: Could someone help me with this code?

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Could someone help me with this code?

    Though my code compiles, it returns all 0's, and I don't understand why.

    The problem I have to answer is :

    Given 2 strings, a and b, return the number of the positions where they contain the same length 2 substring. So "xxcaazz" and "xxbaaz" yields 3, since the "xx", "aa", and "az" substrings appear in the same place in both strings.

    Example:

    stringMatch("xxcaazz", "xxbaaz") → 3
    stringMatch("abc", "abc") → 2
    stringMatch("abc", "axc") → 0

    My code for the method is:

    public int stringMatch(String a, String b)
    {
    int match = 0;
    int x = 0;
    int y = 2;
    while(y < a.length() && y < b.length())
    {
    if (a.substring(x,y) == b.substring(x,y))
    {
    match++;
    x++;
    y++;
    }
    else
    {
    x++;
    y++;
    }
    }
    return match;
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Could someone help me with this code?

    Use the equals() method when comparing the contents of two Strings, not the == operator

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

    espltdf100 (January 7th, 2012)

  4. #3
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Could someone help me with this code?

    Firstly, can you please surround your code [highlight=Java]//your code here[/highlight] like that so it is easier to read?

    Then, I have a question: do the matching substrings have to start at the same index?
    So, for example would

    stringMatch("at", "cat");

    return 1 or 0?
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  5. #4
    Junior Member
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Could someone help me with this code?

    My bad.

    The new code after using the equals method is:

        public int stringMatch(String a, String b) 
        {
            int match = 0;
            int x = 0;
            int y = 2;
            String aa = a.substring(x,y);
            String bb = b.substring(x,y);
            while(y < a.length() && y < b.length())
            {
                if (aa.equals(bb) )
                {
                    match++;
                    x++;
                    y++;
                }
                else
                {
                    x++;
                    y++;
                }
            }
            return match;
        }

    The substrings are supposed to start at the same index.
    Thanks for the responses btw!

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Could someone help me with this code?

    Does it work now?

  7. #6
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Could someone help me with this code?

    String aa = a.substring(x,y);
    String bb = b.substring(x,y);
    You define these at the start of the stringMatch() method, but do you ever change their values? The statement...

    if(aa.equals(bb))

    ...will compare the same thing for each iteration of the while loop unless you change the values of aa and bb.

    Thanks for the responses btw!
    No problem! Happy to help!
    Last edited by snowguy13; January 7th, 2012 at 03:37 PM.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  8. #7
    Junior Member
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Could someone help me with this code?

    It no longer returns all 0s, but I still don't recieve the expected answer.
    The tests are:

    stringMatch("xxcaazz", "xxbaaz") → expected 3, got 4
    stringMatch("abc", "abc") → expected 2, got 1
    stringMatch("abc", "axc") → expected 0, got 0
    stringMatch("hello", "he") → expected 1, got 0
    stringMatch("he", "hello") → expected1, got 0
    stringMatch("aabbccdd", "abbbxxd") → expected 1, got 0
    stringMatch("aaxxaaxx", "iaxxai") → expected 3, got 0
    stringMatch("iaxxai", "aaxxaaxx") → expected 3, got 0

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Could someone help me with this code?

    See post #6

  10. #9
    Junior Member
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Could someone help me with this code?

    The new code is:

    public int stringMatch(String a, String b) 
        {
            int match = 0;
            int x = 0;
            int y = 2;
            String aa = a.substring(x,y);
            String bb = b.substring(x,y);
            while(y < a.length() && y < b.length())
            {
                if (aa.equals(bb) )
                {
                    match++;
                    x++;
                    y++;
                    aa = a.substring(x,y);
                    bb = b.substring(x,y);
                }
                else
                {
                    x++;
                    y++;
                    aa = a.substring(x,y);
                    bb = b.substring(x,y);
                }
            }
            return match;
        }

    I get more correct answers now, though a few still come out incorrect

    stringMatch("xxcaazz", "xxbaaz") → expected 3, got 2
    stringMatch("abc", "abc") → expected 2, got 1
    stringMatch("abc", "axc") → expected 0, got 0
    stringMatch("hello", "he") → expected 1, got 0
    stringMatch("he", "hello") → expected1, got 0
    stringMatch("aabbccdd", "abbbxxd") → expected 1, got 1
    stringMatch("aaxxaaxx", "iaxxai") → expected 3, got 3
    stringMatch("iaxxai", "aaxxaaxx") → expected 3, got 3

  11. #10
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Could someone help me with this code?

    Okay, I think I know what the issue is.

    while(y < a.length() && y < b.length())

    This is tough to explain without giving it away, but here it goes: think about how the substring() method works. If you did a substring from 0 to 1, how many characters of the original String would you get? Now, think about this: if you asked for <String>.substring(0, <String>.length()), how many characters of the String would you get?

    I'm sorry for that inadequate guidance, but I can't just hand you the answer.
    Last edited by snowguy13; January 7th, 2012 at 03:51 PM.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  12. The Following User Says Thank You to snowguy13 For This Useful Post:

    espltdf100 (January 7th, 2012)

  13. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Could someone help me with this code?

    It helps to print out the values of the Strings that are being compared so you can see what the code is doing.
    Add some printlns just before the if test with the equals() to show their values. Something like this:
    System.out.println("aa=" + aa +"<");

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

    espltdf100 (January 7th, 2012)

  15. #12
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Could someone help me with this code?

    Follow Norm's advice. Trust me. It's helped me many times.

    To make my previous post more clear, think about this code:
    String example = "thunderstorm";
    System.out.print(example.substring(0, 3));
    Your output would be:
    thu
    or, to think about it this way...
    thunderstorm

    So what if I did this:
    String example = "thunderstorm";
    System.out.print(example.substring(7, example.length()));
    The output would be:
    storm
    or, to think about it this way...
    thunderstorm

    Using the length of a String as an argument in the substring() method works, but your while statement assumes that it doesn't by saying that the ending index (y) must be less than the String's length.

    ....hopefully that makes more sense...?
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  16. The Following User Says Thank You to snowguy13 For This Useful Post:

    espltdf100 (January 7th, 2012)

  17. #13
    Junior Member
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Could someone help me with this code?

    public int stringMatch(String a, String b) 
        {
            int match = 0;
            int x = 0;
            int y = 2;
            String aa = a.substring(x,y);
            String bb = b.substring(x,y);
            while(y <= a.length() && y <= b.length())
            {
                aa = a.substring(x,y);
                bb = b.substring(x,y);
                if (aa.equals(bb) )
                {
                    match++;
                    x++;
                    y++;
     
                }
                else
                {
                    x++;
                    y++;
     
                }
            }
            return match;
        }

    lol took me a while but I finally got it. My code works everytime unless one of the parameters has less than 2 characters, but I think I know where to go from here. Thanks for the help snowguy and norm, I really appreciate it

  18. #14
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Could someone help me with this code?

    Glad I could help! Good luck with your program!
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

Similar Threads

  1. problem in my code java code graph editeur
    By kisokiso in forum Java Theory & Questions
    Replies: 5
    Last Post: January 6th, 2012, 08:36 AM
  2. Code is giving an error in console, but there are no errors apparent in the code!
    By JamEngulfer221 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2011, 09:30 PM
  3. describe this program code by code ....
    By izzahmed in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 29th, 2011, 11:03 PM