Solved

string surrounding other string

Posted on 2014-11-11
24
168 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
[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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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…
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.

734 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