Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 522
  • Last Modified:

atFirst java challenge

Hi,

I am working on below challenge

http://codingbat.com/prob/p139076
I wrote my code as below

public String atFirst(String str) {
  if(str.length()>=2){
  return str.substring(0,2);
  }
   if(str.length()==1){
  return str.substring(0)+"@";
  }
  else
  {
  return "@";
  }
  
  
  
  }

Open in new window

I am failing below tests.
Expected	Run		
atFirst("hello") → "he"	"he"	OK	    
atFirst("hi") → "hi"	"hi"	OK	    
atFirst("h") → "h@"	"h@"	OK	    
atFirst("") → "@@"	"@"	X	    
atFirst("kitten") → "ki"	"ki"	OK	    
atFirst("java") → "ja"	"ja"	OK	    
atFirst("j") → "j@"	"j@"	OK	    
other tests
OK	    

Open in new window


How to fix and improve my code. Thanks in advance
0
gudii9
Asked:
gudii9
  • 2
2 Solutions
 
Zsolt PribuszCommented:
Insert one more @ in line 10:

return "@@";
0
 
Zsolt PribuszCommented:
Or you can do this challenge with only one line:

public String atFirst(String str) {

  return (str+"@@").substring(0,2);
    
}

Open in new window

0
 
gudii9Author Commented:
return (str+"@@").substring(0,2);

wHAT IS MEANING OF ABOVE return statement. i never used this kind of return statement. please advise
0
 
ozoCommented:
Breaking it down:
 String temp1= str+"@@";
 String temp2=temp1.substring(0,2);
 return temp2;
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.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now