Solved

java string challenge

Posted on 2014-11-14
12
338 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
  • 2
  • +1
12 Comments
 
LVL 17

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 17

Expert Comment

by:krakatoa
ID: 40442536
I see no reason why that should be any clearer. Exactly the opposite in fact.
0
Independent Software Vendors: 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 17

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 17

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 17

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 17

Expert Comment

by:krakatoa
ID: 40443909
You already answered your own question here :  your own comment.
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

691 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