returning two middle characters, favoring the right

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mdthom08
    New Member
    • Sep 2008
    • 3

    returning two middle characters, favoring the right

    I need to return the middle 2 characters of a word, favoring the right.
    so for example:

    Code:
    SevenMethods myFuns = new SevenMethods(); 
    assertEquals("34", myFuns.middleTwo("12345"));
    so far i have:
    Code:
    public String middleTwo(String arg) { 
    	  int middle = arg.length() / 2;
              int middle2 = middle / 2;
    	  String middleTwo = "" + arg.charAt(middle2) + "" + arg.charAt(middle);
        return middleTwo;

    I can't figure out how to favor the right.
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

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

    Code:
    SevenMethods myFuns = new SevenMethods(); 
    assertEquals("34", myFuns.middleTwo("12345"));
    so far i have:
    Code:
    public String middleTwo(String arg) { 
    	  int middle = arg.length() / 2;
              int middle2 = middle / 2;
    	  String middleTwo = "" + arg.charAt(middle2) + "" + arg.charAt(middle);
        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 [code=java]String result = middleTwo("1234 5");[/code], then the following will happen: [code=java]public String middleTwo("1234 5") {
    int middle = "12345".length( ) / 2; // = 5 / 2 = 2 (as you're using integers)
    int middle2 = 2 / 2; // = 1
    String middleTwo = "" + "12345".charAt( 1) + "" + "12345".charAt( 2); // = "" + 2 + "" + 3
    return "23";
    }[/code] 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 lengthOfTheStri ng / 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 lengthOfTheStri ng / 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

    Comment

    • mdthom08
      New Member
      • Sep 2008
      • 3

      #3
      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."

      Comment

      • jx2
        New Member
        • Feb 2007
        • 228

        #4
        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.:
        Code:
        int i = str.length();
        i = i % 2;
        i hope that helps
        Jan Jarczyk

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          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

          Comment

          Working...