Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

java string challenge to display

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

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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

783 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