Results 1 to 9 of 9
  1. #1
    al_saodiyya is offline Member
    Join Date
    Aug 2013
    Location
    Earth, The Milky Way
    Posts
    4
    Rep Power
    0

    Default I do not think my else statement is being executed, why is this?

    CodingBat Java String-1 conCat

    public String conCat(String a, String b) {

    if (a.length() > 0 && b.length() > 0)

    if (a.substring(a.length()-1) != b.substring(0,1)) {

    return a + b; }

    else {

    return a + b.substring(1);

    }

    }

    why doesn't this code work?

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: I do not think my else statement is being executed, why is this?

    Because motorcycles don't have doors!

    Perhaps you can provide more information about "doesn't work" means.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,928
    Rep Power
    4

    Default Re: I do not think my else statement is being executed, why is this?

    I assume you mean it doesn't compile. The reason is because if the first condition is not met, you are returning anything.
    Here is the same code with braces:

    Java Code:
       public String conCat(String a, String b) {
          if (a.length() > 0 && b.length() > 0) {
             if (a.substring(a.length() - 1) != b.substring(0, 1)) {
                return a + b;
             }
             else {
                return a + b.substring(1);
             }
          }
       }
    To catch these types of errors, always use the curly braces with your if statements.

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

  4. #4
    al_saodiyya is offline Member
    Join Date
    Aug 2013
    Location
    Earth, The Milky Way
    Posts
    4
    Rep Power
    0

    Default Re: I do not think my else statement is being executed, why is this?

    CodingBat Java String-1 conCat

    could you please have a look at the link and answer based on the requirements of that question?
    I tried the brackets to no avail.

    thanks.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,928
    Rep Power
    4

    Default Re: I do not think my else statement is being executed, why is this?

    I understand the requirements. Did your program compile or did the site report an error?

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

  6. #6
    al_saodiyya is offline Member
    Join Date
    Aug 2013
    Location
    Earth, The Milky Way
    Posts
    4
    Rep Power
    0

    Default Re: I do not think my else statement is being executed, why is this?

    Error: public String conCat(String a, String b) {
    ^^^^^^^^^^^^^^^^^^^^^^^^^^
    This method must return a result of type String

    I added a return ""; to fix this problem, but now abccat returns abcat
    only dogcat => dogcat works.

  7. #7
    al_saodiyya is offline Member
    Join Date
    Aug 2013
    Location
    Earth, The Milky Way
    Posts
    4
    Rep Power
    0

    Default Re: I do not think my else statement is being executed, why is this?

    *sorry abc cat => abccat
    should return abcat

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: I do not think my else statement is being executed, why is this?

    Quote Originally Posted by al_saodiyya View Post
    CodingBat Java String-1 conCat

    could you please have a look at the link
    Please go through these pages:
    Guide For New Members
    BB Code List - Java Programming Forum - Learn Java Programming

    Then post your code on the forum -- or at least enough of it to demonstrate the problem.

    We don't like forum threads that lose context because the original poster removed the external files that provided that context. A forum is a community resource; let's honor that.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,928
    Rep Power
    4

    Default Re: I do not think my else statement is being executed, why is this?

    I posted the requirements for others on this forum. Please do so in the future.

    Given two strings, append them together (known as "concatenation") and return the result. However, if the concatenation creates a double-char, then omit one of the chars, so "abc" and "cat" yields "abcat".

    conCat("abc", "cat") → "abcat"
    conCat("dog", "cat") → "dogcat"
    conCat("abc", "") → "abc"
    So your statement tells me it is working.

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

Similar Threads

  1. Application only works when executed from netbeans
    By TijVoe in forum Advanced Java
    Replies: 4
    Last Post: 10-02-2012, 03:06 PM
  2. My first java program could not be executed..help
    By miaaa00 in forum New To Java
    Replies: 7
    Last Post: 03-21-2011, 08:07 AM
  3. instruction apparently not executed
    By rippon in forum AWT / Swing
    Replies: 4
    Last Post: 11-30-2010, 01:30 AM
  4. Finally does not get executed
    By rahulkumar in forum New To Java
    Replies: 8
    Last Post: 04-06-2010, 05:30 PM
  5. Will finally get executed...
    By AlmostAGuru in forum New To Java
    Replies: 1
    Last Post: 08-10-2009, 07:12 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
  •