middle three challenge

Hi,

I am working on below challege
http://codingbat.com/prob/p115863

I wrote as below
public String middleThree(String str) {
int len=str.length();
int len2=str.length()/2;

return str.substring(len2-1,len2)+str.substring(len2,len2+1)+str.substring(len2+1,len2+2);
  
}

Open in new window

I passed all tests.

I wonder how to improve my code. Please advise
LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MontoyaProcess Improvement MgrCommented:
try this:

public String middleThree(String str) {
  int half = str.length()/2;
  return str.substring(half-1, half+2);
 
}
0
Zsolt PribuszCommented:
I prefer one liners :)

public String middleThree(String str) {
  return (str.substring((str.length()-3)/2)).substring(0,3);
}

Open in new window

0
gudii9Author Commented:
 return (str.substring((str.length()-3)/2)).substring(0,3);

what is the meaning of above code.  we are doing substring(0,3) on the resultant string of  (str.substring((str.length()-3)/2))?
Please advise
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

MontoyaProcess Improvement MgrCommented:
str.length is the total length of your string, he is simply subtracting 3 and dividing by 2 (cutting it in half)
0
ozoCommented:
What do you need advice on?
Do you understand (str.length()-3)/2 ?
Do you understand str.substring((str.length()-3)/2) ?
0
gudii9Author Commented:
he is simply subtracting 3 and dividing by 2

string length is varying right. why subtracting 3 why not 5 or 7. i am not clear on that. please advise
0
ozoCommented:
Think about the value of  (str.length()-5)/2 or  (str.length()-7)/2 for varying string lengths.
Do any of them give you the value you want?

Or consider the relationship between (str.length()-3)/2 and (str.length()-1)/2-1
or to str.length()/2-1
0
gudii9Author Commented:
str.length is the total length of your string, he is simply subtracting 3 and dividing by 2 (cutting it in half)

i am confused using inner substring. Substring within other substring.

I see requirent deals only with odd number and atleast of 3 digit so taking -3(not -5, -7 or -1 or -9 etc) makes more sense as below

str.length()-3)/2

can you please break down the inner substring to more understandable steps. please advise
0
ozoCommented:
Maybe it would help if you wrote down a list of different string lengths, and the corresponding substring position you would want in each case.
A relationship should become apparent.
0
gudii9Author Commented:
Think about the value of  (str.length()-5)/2 or  (str.length()-7)/2 for varying string lengths.
Do any of them give you the value you want?

if string length is 3 above results -2/2=1 or -4/2=-2 again which is not what we want


Or consider the relationship between (str.length()-3)/2 and (str.length()-1)/2-1
or to str.length()/2-1

(str.length()-3)/2 ----0/2 which yields  0
Maybe it would help if you wrote down a list of different string lengths, and the corresponding substring position you would want in each case.
if length is 3 i want character at index 1 which is middle and one before and after so xyz(xyz)
if length is 5 i want character at index 2and one before and after so yza (xyzab
if length is 7 i want character at index 3and one before and after so zab(xyzabcd)
if length is 9i want character at index 4and one before and after so  (xyzabcdef
0
ozoCommented:
Do you see any relationship connecting
3 -> 1 and one before and after
5 -> 2 and one before and after
7 -> 3 and one before and after
9 -> 4 and one before and after
to
(str.length()-3)/2
?
0
gudii9Author Commented:
str.length()-3 when you subract by three always get even number like 0,2,4,6 respectively for string lenghts 3,5,7,9

when you devide even number by 2 then reminder always 0 and the value that is yielded is 0,1,2,3
otherthank that i do not see any other particular relationship. please advise
0
ozoCommented:
Can you think of a way to use the values 0,1,2,3 ?
0
gudii9Author Commented:
so on resultant 0,1,2,3 the are doing other substring(0,3)

like
substring(0).substring(0,3)
substring(1).substring(0,3)
substring(2).substring(0,3)
substring(3).substring(0,3)

please advise
0
ozoCommented:
So if (str.length()-3)/2 for lenghts 3,5,7,9 yields the values 0,1,2,3
you seem to be saying that str.substring((str.length()-3)/2).substring(0,3)
could do something desireable.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gudii9Author Commented:
yes
0
gudii9Author Commented:
i think now i understand it.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.