Solved

java string challenge

Posted on 2014-11-14
12
327 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
0
Comment
Question by:gudii9
  • 6
  • 2
  • 2
  • +1
12 Comments
 
LVL 16

Assisted Solution

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

0
 
LVL 84

Accepted Solution

by:
ozo earned 250 total points
Comment Utility
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);
0
 
LVL 16

Expert Comment

by:krakatoa
Comment Utility
I see no reason why that should be any clearer. Exactly the opposite in fact.
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
If a redundant ==true adds clarity, does that make (front==true)==true even more clear?
0
 
LVL 16

Expert Comment

by:krakatoa
Comment Utility
Very funny. But not terribly amusing or informative. You choose your level of clarity.
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
 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?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 31

Expert Comment

by:awking00
Comment Utility
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.
0
 
LVL 16

Expert Comment

by:krakatoa
Comment Utility
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?
0
 
LVL 31

Expert Comment

by:awking00
Comment Utility
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.
0
 
LVL 16

Expert Comment

by:krakatoa
Comment Utility
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.
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
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.
0
 
LVL 16

Expert Comment

by:krakatoa
Comment Utility
You already answered your own question here :  your own comment.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

772 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

14 Experts available now in Live!

Get 1:1 Help Now