Results 1 to 9 of 9
- 08-23-2013, 04:36 AM #1
Member
- Join Date
- Aug 2013
- Location
- Earth, The Milky Way
- Posts
- 4
- Rep Power
- 0
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?
- 08-23-2013, 04:53 AM #2
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.
- 08-23-2013, 04:55 AM #3
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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); } } }
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 08-23-2013, 05:15 AM #4
Member
- Join Date
- Aug 2013
- Location
- Earth, The Milky Way
- Posts
- 4
- Rep Power
- 0
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.
- 08-23-2013, 05:27 AM #5
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 08-23-2013, 05:33 AM #6
Member
- Join Date
- Aug 2013
- Location
- Earth, The Milky Way
- Posts
- 4
- Rep Power
- 0
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.
- 08-23-2013, 05:33 AM #7
Member
- Join Date
- Aug 2013
- Location
- Earth, The Milky Way
- Posts
- 4
- Rep Power
- 0
Re: I do not think my else statement is being executed, why is this?
*sorry abc cat => abccat
should return abcat
- 08-23-2013, 05:42 AM #8
Re: I do not think my else statement is being executed, why is this?
Please go through these pages:
http://www.java-forums.org/forum-gui...w-members.html
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.
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 08-23-2013, 05:49 AM #9
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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"
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
Similar Threads
-
Application only works when executed from netbeans
By TijVoe in forum Advanced JavaReplies: 4Last Post: 10-02-2012, 03:06 PM -
My first java program could not be executed..help
By miaaa00 in forum New To JavaReplies: 7Last Post: 03-21-2011, 08:07 AM -
instruction apparently not executed
By rippon in forum AWT / SwingReplies: 4Last Post: 11-30-2010, 01:30 AM -
Finally does not get executed
By rahulkumar in forum New To JavaReplies: 8Last Post: 04-06-2010, 05:30 PM -
Will finally get executed...
By AlmostAGuru in forum New To JavaReplies: 1Last Post: 08-10-2009, 07:12 PM
Bookmarks