Solved

java string challenge

Posted on 2014-11-14
12
337 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
ID: 40442475
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
ID: 40442519
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
ID: 40442536
I see no reason why that should be any clearer. Exactly the opposite in fact.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

Expert Comment

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

Expert Comment

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

Author Comment

by:gudii9
ID: 40443150
 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
 
LVL 32

Expert Comment

by:awking00
ID: 40443228
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
ID: 40443261
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 32

Expert Comment

by:awking00
ID: 40443302
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
ID: 40443341
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
ID: 40443397
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
ID: 40443909
You already answered your own question here :  your own comment.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java: The Public Class Main 4 45
java example issue 5 45
Cisco ASA: Java web start no go, asdm launcher no go 3 58
convert Systemjs to Webpack 3 82
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

733 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