string surrounding other string

Hi,

I am trying below challenge
http://codingbat.com/prob/p168564
i have tried as below
public String comboString(String a, String b) {
String str2=null;
int i= a.length();
int j=b.length();
  if(i>0 && j>0)
  String str2=a+b+a;
  return str2;
}

Open in new window


i am getting below error

Compile problems:


Error:      String str2=a+b+a;
      ^^^^^^
Syntax error, insert "AssignmentOperator ArrayInitializer" to complete ArrayInitializerAssignement

How to fix and improve my code. Thanks in advance
LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
str2 was already declared str2 to be a String in line 2.   It doesn't need another String declaration in line 6,
and java doesn't understand a declaration right after a )
0
gudii9Author Commented:
public String comboString(String a, String b) {
String str2=null;
int i= a.length();
int j=b.length();
  if(i>0 && j>0)
  str2=a+b+a;
  return str2;
}

Open in new window


i modified my code and it is still failing in some test cases. please advise
Expected      Run            
comboString("Hello", "hi") → "hiHellohi"      "HellohiHello"      X         
comboString("hi", "Hello") → "hiHellohi"      "hiHellohi"      OK         
comboString("aaa", "b") → "baaab"      "aaabaaa"      X         
comboString("b", "aaa") → "baaab"      "baaab"      OK         
comboString("aaa", "") → "aaa"      "null"      X         
comboString("", "bb") → "bb"      "null"      X         
comboString("aaa", "1234") → "aaa1234aaa"      "aaa1234aaa"      OK         
comboString("aaa", "bb") → "bbaaabb"      "aaabbaaa"      X         
comboString("a", "bb") → "abba"      "abba"      OK         
comboString("bb", "a") → "abba"      "bbabb"      X         
comboString("xyz", "ab") → "abxyzab"      "xyzabxyz"      X         
other tests
OK
0
ozoCommented:
Sometimes you are returning long+short+long, with the longer string on the outside and the  shorter string on the inside.

And sometimes you are returning "null".
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

gudii9Author Commented:
public String comboString(String a, String b) {
String str2=null;
int i= a.length();
int j=b.length();
  if(i>0 && j>0 && j>i){
  str2=a+b+a;
  return str2;
  }
  
   else(i>0 && j>0 && i>j){
  str2=b+a+b;
  return str2;
  }
}

Open in new window


i modified as above and getting below error. please advise
Error:      else(i>0 && j>0 && i>j){
                            ^
Syntax error, insert "AssignmentOperator ArrayInitializer" to complete ArrayInitializerAssignement
0
gudii9Author Commented:
public String comboString(String a, String b) {
String str2=null;
String str3=null;
int i= a.length();
int j=b.length();
  if(i>0 && j>0 && j>i){
  str2=a+b+a;
  return str2;
  }
  
   else(i>0 && j>0 && i>j){
  str3=b+a+b;
  return str3;
  }
}

Open in new window


i changed as above still getting same error
0
ozoCommented:
That is not the way you write an if-then-else Statement https://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html
0
gudii9Author Commented:
i know now to not put condition after else

i modified as below
public String comboString(String a, String b) {
String str2=null;
String str3=null;
int i= a.length();
int j=b.length();
  if(i>0 && j>0 && j>i){
  str2=a+b+a;
  return str2;
  }
  
   else if(i>0 && j>0 && i>j){
  str3=b+a+b;
  return str3;
  }
  
   else if(i==0 && j>0){
  str3=b;
  return str3;
  }
  
   else if(i>0 && j==0){
  str3=b+a+b;
  return str3;
  }
  
  
  return null;
}

Open in new window

all tests pass.
does my code looks fine?
please advise
0
ozoCommented:
It is functionally fine.
But it uses 5 different cases when 2 would be sufficient.
0
gudii9Author Commented:
But it uses 5 different cases when 2 would be sufficient.
How to write with 2 cases. please advise
0
ozoCommented:
Which string is shorter?
0
gudii9Author Commented:
comboString("Hello", "hi") → "hiHellohi"
comboString("hi", "Hello") → "hiHellohi"
comboString("aaa", "b") → "baaab"
..

it is varying. sometimes first sometimes second string is shorter(as third example above)
0
gudii9Author Commented:
public String comboString(String a, String b) {
String result="";
if(a.length()> 0&& b.length()>0)
{
if(a.length()>b.length()){
result=b+a+b;
}
else 
result=a+b+a;
}
  return result;
}

Open in new window


I tried modifying as above but failing on some test cases. please advise
Expected	Run		
comboString("Hello", "hi") → "hiHellohi"	"hiHellohi"	OK	    
comboString("hi", "Hello") → "hiHellohi"	"hiHellohi"	OK	    
comboString("aaa", "b") → "baaab"	"baaab"	OK	    
comboString("b", "aaa") → "baaab"	"baaab"	OK	    
comboString("aaa", "") → "aaa"	""	X	    
comboString("", "bb") → "bb"	""	X	    
comboString("aaa", "1234") → "aaa1234aaa"	"aaa1234aaa"	OK	    
comboString("aaa", "bb") → "bbaaabb"	"bbaaabb"	OK	    
comboString("a", "bb") → "abba"	"abba"	OK	    
comboString("bb", "a") → "abba"	"abba"	OK	    
comboString("xyz", "ab") → "abxyzab"	"abxyzab"	OK	    
other tests
OK	    

Open in new window

0
gudii9Author Commented:
public String comboString(String a, String b) {
String result="";
if(a.length()> 0&& b.length()>0)
{
if(a.length()>b.length()){
result=b+a+b;
}
else 
result=a+b+a;
}

else if(a.length()==0){

result=b;
}
else if(b.length()==0){

result=a;
}
  return result;
}

Open in new window


I fixed my code as above
Expected	Run		
comboString("Hello", "hi") → "hiHellohi"	"hiHellohi"	OK	    
comboString("hi", "Hello") → "hiHellohi"	"hiHellohi"	OK	    
comboString("aaa", "b") → "baaab"	"baaab"	OK	    
comboString("b", "aaa") → "baaab"	"baaab"	OK	    
comboString("aaa", "") → "aaa"	"aaa"	OK	    
comboString("", "bb") → "bb"	"bb"	OK	    
comboString("aaa", "1234") → "aaa1234aaa"	"aaa1234aaa"	OK	    
comboString("aaa", "bb") → "bbaaabb"	"bbaaabb"	OK	    
comboString("a", "bb") → "abba"	"abba"	OK	    
comboString("bb", "a") → "abba"	"abba"	OK	    
comboString("xyz", "ab") → "abxyzab"	"abxyzab"	OK	    
other tests
OK

Open in new window


But
But it uses 5 different cases when 2 would be sufficient.

i was not able to shorten to 2 cases. please advise
0
ozoCommented:
if( a.length()>b.length() ){
    return b+a+b;
}else{
    return a+b+a;
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gudii9Author Commented:
But they are not covering case where strings could be empty.  Please advise
0
ozoCommented:
They are covering case where strings could be empty.
Do you see what happens in such cases?
0
gudii9Author Commented:
a.length()>b.length()

ifboth are empty sting then a and b length becomes 0 i believe so
0>0 is false wo the flow will not go inside the if loop?

Is my understanding is correct?
0
ozoCommented:
0>0 is false so the flow will go through the else block
but you could also have used a.length()>=b.length()
which would have sent the flow through the if block
0
gudii9Author Commented:
but you could also have used a.length()>=b.length()

If i say >= then if a.length() , b.length() are same let us say 3 like a string is "cap" and b string is like "hat" then does it supposed to go to if or else loop to display b+a+b or a+b+a?
please advise
0
ozoCommented:
What does your code do if a.length() , b.length() are same?
What does the challenge want you to do if a.length() , b.length() are same?
0
gudii9Author Commented:
What does the challenge want you to do if a.length() , b.length() are same?
\

it says nothing about it.

All challenge says is


Given 2 strings, a and b, return a string of the form short+long+short, with the shorter string on the outside and the longer string on the inside. The strings will not be the same length, but they may be empty (length 0).

comboString("Hello", "hi") → "hiHellohi"
comboString("hi", "Hello") → "hiHellohi"
comboString("aaa", "b") → "baaab"
actually they say
The strings will not be the same length,
and also
but they may be empty
0
ozoCommented:
Since the challenge imposes no requirement on what happens when a.length() and b.length() are both 3,
it provides preference between > and >=
0
gudii9Author Commented:
it provides preference between > and >=
how to prefer between above two. please advise
0
ozoCommented:
The challenge doesn't care, so it is up to you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.