Solved

java challenge concatenation

Posted on 2014-11-14
12
300 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 325 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 75 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

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

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.

Question has a verified solution.

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

When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
The viewer will learn how to implement Singleton Design Pattern in Java.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

630 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