Solved

atFirst java challenge

Posted on 2014-12-04
4
433 Views
Last Modified: 2014-12-05
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
Comment
Question by:gudii9
  • 2
4 Comments
 
LVL 4

Expert Comment

by:Zsolt Pribusz
Comment Utility
Insert one more @ in line 10:

return "@@";
0
 
LVL 4

Accepted Solution

by:
Zsolt Pribusz earned 250 total points
Comment Utility
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
 
LVL 7

Author Comment

by:gudii9
Comment Utility
return (str+"@@").substring(0,2);

wHAT IS MEANING OF ABOVE return statement. i never used this kind of return statement. please advise
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 250 total points
Comment Utility
Breaking it down:
 String temp1= str+"@@";
 String temp2=temp1.substring(0,2);
 return temp2;
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

The purpose of this article is to demonstrate how we can use conditional statements using Python.
A short article about problems I had with the new location API and permissions in Marshmallow
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

728 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

13 Experts available now in Live!

Get 1:1 Help Now