sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
mdthom08's Avatar

returning two middle characters, favoring the right


Question posted by: mdthom08 (Newbie) on September 21st, 2008 10:46 PM
I need to return the middle 2 characters of a word, favoring the right.
so for example:

Expand|Select|Wrap|Line Numbers
  1. SevenMethods myFuns = new SevenMethods(); 
  2. assertEquals("34", myFuns.middleTwo("12345")); 


so far i have:
Expand|Select|Wrap|Line Numbers
  1. public String middleTwo(String arg) { 
  2.       int middle = arg.length() / 2;
  3.           int middle2 = middle / 2;
  4.       String middleTwo = "" + arg.charAt(middle2) + "" + arg.charAt(middle);
  5.     return middleTwo; 



I can't figure out how to favor the right.
4 Answers Posted
Nepomuk's Avatar
Nepomuk September 21st, 2008 11:06 PM
Moderator - 1,810 Posts
#2: Re: returning two middle characters, favoring the right

Quote:
Originally Posted by mdthom08
I need to return the middle 2 characters of a word, favoring the right.
so for example:

Expand|Select|Wrap|Line Numbers
  1. SevenMethods myFuns = new SevenMethods(); 
  2. assertEquals("34", myFuns.middleTwo("12345")); 


so far i have:
Expand|Select|Wrap|Line Numbers
  1. public String middleTwo(String arg) { 
  2.       int middle = arg.length() / 2;
  3.           int middle2 = middle / 2;
  4.       String middleTwo = "" + arg.charAt(middle2) + "" + arg.charAt(middle);
  5.     return middleTwo; 



I can't figure out how to favor the right.
OK, let's just test your code for a moment.

Say, you call
Expand|Select|Wrap|Line Numbers
  1. String result = middleTwo("12345");
, then the following will happen:
Expand|Select|Wrap|Line Numbers
  1. public String middleTwo("12345") {
  2.       int middle = "12345".length() / 2; // = 5 / 2 = 2 (as you're using integers)
  3.       int middle2 = 2 / 2; // = 1
  4.       String middleTwo = "" + "12345".charAt(1) + "" + "12345".charAt(2); // = "" + 2 + "" + 3
  5.    return "23";
  6. }
That isn't what you want, is it? You want it to return "34".

So, let's have a look at the possible options:
  1. You give the middleTwo() method a String with an odd number of characters
  2. You give the middleTwo() method a String with an even number of characters
With option a. (e.g. "12345"), the middle will be lengthOfTheString / 2 - 0.5 (so, with a String of the length 5, that would be 2) and you will want that and the following character (as Java starts counting at 0).
With option b. (e.g. "123456"), the middle will be lengthOfTheString / 2 exactly, so in the case of 6 characters, it will be 3. You want character 2 and 3. So, can you find a formula to always get the first of the characters you want? Then you just have to write that and the following character.

Greetings,
Nepomuk
mdthom08's Avatar
mdthom08 September 21st, 2008 11:31 PM
Newbie - 3 Posts
#3: Re: returning two middle characters, favoring the right

Thanks. I understand it alot better now.
How can I distinguish from even or odd number of characters so I can use "if" and "else."
jx2's Avatar
jx2 September 22nd, 2008 06:44 AM
Familiar Sight - 218 Posts
#4: Re: returning two middle characters, favoring the right

Quote:
Originally Posted by mdthom08
Thanks. I understand it alot better now.
How can I distinguish from even or odd number of characters so I can use "if" and "else."

the easiest way to do it is to divide by 2 and check if there is a ramiaining
i.e.:
Expand|Select|Wrap|Line Numbers
  1. int i = str.length();
  2. i = i % 2;

i hope that helps
Jan Jarczyk
JosAH's Avatar
JosAH September 22nd, 2008 09:30 AM
Moderator - 8,361 Posts
#5: Re: returning two middle characters, favoring the right

Quote:
Originally Posted by mdthom08
Thanks. I understand it alot better now.
How can I distinguish from even or odd number of characters so I can use "if" and "else."


There is no need to distinguish the two alternatives; the integer division operator
does it all, i.e. the first character you want is at position (s.length()-1)/2. Go and
figure out the details.

kind regards,

Jos
Reply
Not the answer you were looking for? Post your question . . .
197,023 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 197,023 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Java Contributors