Solved

java string challenge

Posted on 2014-11-14
12
336 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb wildfly example 2 22
difference between sorce folder and folder in eclipise 3 28
printf performancy 11 69
collection output issue 9 36
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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

856 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