Solved

string surrounding other string

Posted on 2014-11-11
24
165 Views
Last Modified: 2014-11-14
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
0
Comment
Question by:gudii9
  • 13
  • 11
24 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 40435478
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
 
LVL 7

Author Comment

by:gudii9
ID: 40435690
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
 
LVL 84

Expert Comment

by:ozo
ID: 40435698
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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 7

Author Comment

by:gudii9
ID: 40435715
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
 
LVL 7

Author Comment

by:gudii9
ID: 40435720
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
 
LVL 84

Expert Comment

by:ozo
ID: 40435727
That is not the way you write an if-then-else Statement https://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html
0
 
LVL 7

Author Comment

by:gudii9
ID: 40435876
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
 
LVL 84

Expert Comment

by:ozo
ID: 40436528
It is functionally fine.
But it uses 5 different cases when 2 would be sufficient.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40437363
But it uses 5 different cases when 2 would be sufficient.
How to write with 2 cases. please advise
0
 
LVL 84

Expert Comment

by:ozo
ID: 40437452
Which string is shorter?
0
 
LVL 7

Author Comment

by:gudii9
ID: 40437653
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
 
LVL 7

Author Comment

by:gudii9
ID: 40440408
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
 
LVL 7

Author Comment

by:gudii9
ID: 40440431
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
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 40441382
if( a.length()>b.length() ){
    return b+a+b;
}else{
    return a+b+a;
}
0
 
LVL 7

Author Comment

by:gudii9
ID: 40441453
But they are not covering case where strings could be empty.  Please advise
0
 
LVL 84

Expert Comment

by:ozo
ID: 40441681
They are covering case where strings could be empty.
Do you see what happens in such cases?
0
 
LVL 7

Author Comment

by:gudii9
ID: 40442672
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
 
LVL 84

Expert Comment

by:ozo
ID: 40443555
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
 
LVL 7

Author Comment

by:gudii9
ID: 40443569
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
 
LVL 84

Expert Comment

by:ozo
ID: 40443595
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
 
LVL 7

Author Comment

by:gudii9
ID: 40443637
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
 
LVL 84

Expert Comment

by:ozo
ID: 40443646
Since the challenge imposes no requirement on what happens when a.length() and b.length() are both 3,
it provides preference between > and >=
0
 
LVL 7

Author Comment

by:gudii9
ID: 40443670
it provides preference between > and >=
how to prefer between above two. please advise
0
 
LVL 84

Expert Comment

by:ozo
ID: 40443705
The challenge doesn't care, so it is up to you.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
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…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

828 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