Solved

java string challenge to display

Posted on 2014-11-14
3
300 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 31

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

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

Join & Write a Comment

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

746 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

15 Experts available now in Live!

Get 1:1 Help Now