Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

java challenge concatenation

Posted on 2014-11-14
12
Medium Priority
?
308 Views
Last Modified: 2014-11-20
Hi,

I am working on below coding challenge
http://codingbat.com/prob/p143825
i wrote as below


public String nonStart(String a, String b) {
  String result=null;
  String aFirst=null;
  String bFirst=null;
 
  if(a.length()>1) {
  result=a.substring(1,a.length());
  }
  else{
 
  return "";
  }
  if(b.length()>1) {
  result+=b.substring(1,b.length());
  }
 
  return result;
}


My test cases are passing
i would like to know how can improve on my above code. Please advise.Thanks in advance
0
Comment
Question by:gudii9
12 Comments
 
LVL 17

Accepted Solution

by:
krakatoa earned 1300 total points
ID: 40442490
Shorter would be :

public String nonStart(String a, String b) {
  return a.substring(1)+b.substring(1);
}

Open in new window

0
 
LVL 85

Assisted Solution

by:ozo
ozo earned 300 total points
ID: 40442527
your code is not passing
nonStart("x", "ac") → "c"      ""      X
0
 
LVL 17

Expert Comment

by:krakatoa
ID: 40442541
your code

would always be good for everyone if we knew who you are referring to each time. I remember we had this ID issue once before.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 17

Expert Comment

by:krakatoa
ID: 40442563
@gudii -

You don't need to test the lengths, that's a waste of time.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40442822
i do not need to test lengths since they said strings are atleast of length 1 right in the challenge?


nonStart("x", "ac") → "c"      ""      X

I wonder why code i wrote is failing above test. please advise
public String nonStart(String a, String b) {
  String result=null;
  String aFirst=null;
  String bFirst=null;
  
  if(a.length()>1) {
  result=a.substring(1,a.length());
  }
  else{
  
  return "";
  }
  if(b.length()>1) {
  result+=b.substring(1,b.length());
  }
 
  return result;
}

Open in new window


Expected	Run		
nonStart("Hello", "There") → "ellohere"	"ellohere"	OK	    
nonStart("java", "code") → "avaode"	"avaode"	OK	    
nonStart("shotl", "java") → "hotlava"	"hotlava"	OK	    
nonStart("ab", "xy") → "by"	"by"	OK	    
nonStart("ab", "x") → "b"	"b"	OK	    
nonStart("x", "ac") → "c"	""	X	    
nonStart("a", "x") → ""	""	OK	    
nonStart("kit", "kat") → "itat"	"itat"	OK	    
nonStart("mart", "dart") → "artart"	"artart"	OK	    
other tests
OK	    

Open in new window

when  a string is length 1 it failed. when b string is length 1 no issues. I thought if it failed when a is length 1 then it should fail test cases when b is length 1. please advise
0
 
LVL 17

Expert Comment

by:krakatoa
ID: 40442891
The substring of an empty string is an empty string.

You don't need to test the length of the strings, because the method feeds you the strings into your code - all you need to do is to capture all of both strings you are given,  MINUS the first character in each, and put them together. If there is no first character in the first string, (i.e. there is no string at all), then "nothing" gets added to the second string after the second string is stripped of its first character. And if the second string also has no first character (so it's blank), then you return an empty string, which is fine.

If one of them or both have 1 or fewer characters, then an empty string is returned.

If a or b or both has more than 1 character (i.e. 2 or more), then you return the two substrings concatenated after character 0 of each until the end of each string.
0
 
LVL 17

Expert Comment

by:krakatoa
ID: 40442969
And so here is your code again, corrected to that end :

public String nonStart(String a, String b) {
  //String result=null;
 //String aFirst=null;
  //String bFirst=null;
  
  //if(a.length()>1) {
  //result=a.substring(1,a.length());
  //}
  //else{
  
  //return "";
  //}
  //if(b.length()>1) {
  //result+=b.substring(1,b.length());
  //}
 
  return a.substring(1)+b.substring(1);

}

Open in new window

0
 
LVL 32

Assisted Solution

by:awking00
awking00 earned 400 total points
ID: 40444464
gudii9,
I answered this before in question Q_28562074.html, which said if the length of string a = 1 and the length of string b > 1, then just return the substring of b, and if length of string b = 1 and the length of string a > 1, then just return the substring of a, and if the lengths of both a and b are > 1, then return the concatenation of the substrings of a and b., else return null.

krakatoa,
>>If one of them or both have 1 or fewer characters, then an empty string is returned<<
I don't think this is correct. If one of them has 1 character and the other is greater than 1, it should return the substring of the one that was greater than 1.

All,
I believe this is correct and what seems to be missing from the other solutions.
0
 
LVL 17

Expert Comment

by:krakatoa
ID: 40444466
krakatoa,
 >>If one of them or both have 1 or fewer characters, then an empty string is returned<<
 I don't think this is correct. If one of them has 1 character and the other is greater than 1, it should return the substring of the one that was greater than 1.

. . .  you've got to re-read what you have said here, and see that it doesn't make any sense. I said that if one of them or both have 1 or fewer characters  . . . . . . . so why have you said that " . .  and the other is greater than 1, . ." ??

I'd actually prefer to hear from the OP - gudii - on this before we become entangled any further. gudii : all you need to do is to understand why I commented-out nearly all of your code in your last post, and left in just one line with the return value. This question is far simpler than you seem to think it is, so please just re-read the question, and compare it to what is going on in that return statement I have given you.
0
 
LVL 17

Expert Comment

by:krakatoa
ID: 40445073
(Correction :

In my post ID: 40442891

I said "The substring of an empty string is an empty string." That's true only when you take a substring of an empty string at index 0. Otherwise you'll get an out of bounds exception if you try any + or - index.)
0
 
LVL 7

Author Comment

by:gudii9
ID: 40455259
public String nonStart(String a, String b) {
  //String result=null;
 //String aFirst=null;
  //String bFirst=null;
  
  //if(a.length()>1) {
  //result=a.substring(1,a.length());
  //}
  //else{
  
  //return "";
  //}
  //if(b.length()>1) {
  //result+=b.substring(1,b.length());
  //}
 
  return a.substring(1)+b.substring(1);

}

Open in new window


i liked above solution. simple and straight forward. Basically returning substring on a and b starting index 1 till  all the way end.
0
 
LVL 17

Expert Comment

by:krakatoa
ID: 40455457
Right. Pleased it met with your approval.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question