We help IT Professionals succeed at work.

java string challenge

418 Views
Last Modified: 2014-11-14
Hi,

I am working on below challenge
http://codingbat.com/prob/p162477
i wrote as below
public String theEnd(String str, boolean front) {
if(front){
return str.substring(0,1);
}
  else{
  return str.substring(str.length()-1);

}
  
}

Open in new window


i would like to know how can improve on my above code. Thanks in advacne
Comment
Watch Question

CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT

Commented:
I see no reason why that should be any clearer. Exactly the opposite in fact.
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
If a redundant ==true adds clarity, does that make (front==true)==true even more clear?
CERTIFIED EXPERT

Commented:
Very funny. But not terribly amusing or informative. You choose your level of clarity.

Author

Commented:
 int p=front?1:str.length();
  return str.substring(p-1,p);

can you please elaborate on this one. If front is true then p becomes 1 otherwise it becomes str.length().
And you are taking that p inside substring method?
awking00Information Technology Specialist
CERTIFIED EXPERT

Commented:
While I like using the ternary operator in if-then-else operations, I don't know if it constitutes and "improvement" over what gudii9 wrote, which is perfectly fine.
CERTIFIED EXPERT

Commented:
While I like using the ternary operator . . .

I didn't say it was an improvement. I said :

You could also :

And improvements are classified how, would you say?
awking00Information Technology Specialist
CERTIFIED EXPERT

Commented:
I didn't mean to infer you thought it was an improvement, just that the asker stated "i would like to know how can improve on my above code." I was trying to say that, as a practice, personally I would prefer the ternary method that you offered, but the asker's method was perfectly okay as well.
CERTIFIED EXPERT

Commented:
Well by my metrics, gudii's code is roughly twice as fast as ozo's solution, while my ternary is four times faster very roughly. I don't know if that could be considered an improvement that would justify my comment.

If not, then I suppose I merely made a comment.

Author

Commented:
int p=front?1:str.length();
  return str.substring(p-1,p);


can you please elaborate on this one. If front is true then p becomes 1 otherwise it becomes str.length().
And you are taking that p inside substring method?

can you please advise on usage of p here. I wonder how this solution is related to the code i wrote.
CERTIFIED EXPERT

Commented:
You already answered your own question here :  your own comment.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.