Solved

java string challenge to display

Posted on 2014-11-14
3
321 Views
Last Modified: 2014-11-14
Hi,

I am working on below coding challenge
http://codingbat.com/prob/p130781
i wrote as below


public String right2(String str) {

return str.substring((str.length())-2)+str.substring(0,str.length()-2);
  
}

Open in new window

i would like to know how can improve on my above code. Please advise.Thanks in advance
0
Comment
Question by:gudii9
3 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 250 total points
ID: 40442505
Code seems fine.
The inconsistency whether str.length() is parenthesized could add confusion.
You may prefer to compute str.length()-2 only once,
on the other hand str.length()-2 is so simple to compute that you may feel that an extra variable
adds more complication.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40442805
public String right2(String str) {
int len=str.length();
return str.substring(len-2)+str.substring(0,len-2);
  
}

Open in new window


i like idea of creating variable to compute once as above
0
 
LVL 32

Assisted Solution

by:awking00
awking00 earned 250 total points
ID: 40443441
Doing that can often make it easier to understand as well. It might be even more meaningful if the variable is more descriptive. Since what you really want to do is substring from the index of the next to last character to the end concatenated with the substring from the beginning index to the index of the next to last character, you might want to name your variable idx and give it the index value -
int idx = str.length() - 2;
return str.substring(idx) + str.substring(0,idx);
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why my table column Id is not passed to java object? 4 44
SequenceInputStream example 3 19
Problem to Alipay 10 44
Java 8 to Java 6 8 20
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
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 step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

861 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