Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

java challenge concatenation

Posted on 2014-11-14
12
Medium Priority
?
304 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 16

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 84

Assisted Solution

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

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
A new era in Cloud training has arrived.

A day that will go down in Cloud history.. But are you ready for it? Will you accept this Cloud challenge?

 
LVL 16

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 16

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 16

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 16

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 16

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 16

Expert Comment

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

Featured Post

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

721 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