Solved

java string challenge to display

Posted on 2014-11-14
3
347 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
[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
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

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

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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.
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.

623 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