Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

java string challenge to display

Posted on 2014-11-14
3
Medium Priority
?
351 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 1000 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 1000 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

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!

Question has a verified solution.

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

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 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.
Suggested Courses

715 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