473,396 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

returning two middle characters, favoring the right

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.
Sep 21 '08 #1
4 7643
Nepomuk
3,112 Expert 2GB
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
Sep 21 '08 #2
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."
Sep 21 '08 #3
jx2
228 100+
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;
  3.  
i hope that helps
Jan Jarczyk
Sep 22 '08 #4
JosAH
11,448 Expert 8TB
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
Sep 22 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

20
by: Ken Godee | last post by:
module1 calls a function in module2 module2 starts a thread that calls a function in module3 and then returns to module1 thread finishes and I need the return value from the thread to use in...
7
by: Andy Fish | last post by:
Hi, I have a servlet (running under tomcat 4.1, java 1.4.2) that sends XML in the HTTP body from a servlet. The I want the XML to be encoded in UTF-8. when I run Tomcat on windows 2000, the...
6
by: Tom | last post by:
Hi, In the following code I have reproduced a problem in IE that is extremely annoying for my application. <html> <head> <script type="text/javascript">
17
by: Olivier Bellemare | last post by:
I've tried to make a function that returns the middle of a string. For example: strmid("this is a text",6,4); would return "is a". Here is my code: char *strmid(char *texte, int depart,...
7
by: tano | last post by:
Hello, I have to insert a char in the middle of a string, I have written two functions but I don't know what is the better? The problem is: if I use malloc() I copy all the string with the new...
10
by: Pete | last post by:
Can someone please help, I'm trying to pass an array to a function, do some operation on that array, then return it for further use. The errors I am getting for the following code are, differences...
4
by: Mantorok | last post by:
Hi I have a couple of encryption methods but when I call decrypt I get the string back but with a load \0 escape characters on the end? Any idea why? It is actually causing problems in some...
1
by: Anandan | last post by:
Hi, This is regarding Dataset Filter: WILDCARD CHARACTERS Both the * and % can be used interchangeably for wildcards in a LIKE comparison. If the string in a LIKE clause contains a * or %,...
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.