Solved

string surrounding other string

Posted on 2014-11-11
24
149 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 7

Author Comment

by:gudii9
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
It is functionally fine.
But it uses 5 different cases when 2 would be sufficient.
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
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
Comment Utility
Which string is shorter?
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 7

Author Comment

by:gudii9
Comment Utility
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
Comment Utility
if( a.length()>b.length() ){
    return b+a+b;
}else{
    return a+b+a;
}
0
 
LVL 7

Author Comment

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

Expert Comment

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

Author Comment

by:gudii9
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
it provides preference between > and >=
how to prefer between above two. please advise
0
 
LVL 84

Expert Comment

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

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now