java string challenge

gudii9
gudii9 used Ask the Experts™
on
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

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
You could also :

public String theEnd(String str, boolean front) {
  return (front==true?str.substring(0,1):str.substring(str.length()-1));
}

Open in new window

Most Valuable Expert 2014
Top Expert 2015
Commented:
I would think  front?str.substring(0,1):str.substring(str.length()-1)
would be clearer than front==true?str.substring(0,1):str.substring(str.length()-1)
Another variant could be
  int p=front?1:str.length();
  return str.substring(p-1,p);
I see no reason why that should be any clearer. Exactly the opposite in fact.
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
If a redundant ==true adds clarity, does that make (front==true)==true even more clear?
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

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.
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

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.
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.
You already answered your own question here :  your own comment.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start Today