Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

java challenge concatenation

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
ASKER CERTIFIED SOLUTION
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
@gudii -

You don't need to test the lengths, that's a waste of time.
Avatar of gudii9

ASKER

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
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.
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

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
(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.)
Avatar of gudii9

ASKER

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.
Right. Pleased it met with your approval.