Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Trouble with getting middle characters from a string.

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Trouble with getting middle characters from a string.

    Can someone please help and tell me what i am doing wrong that makes my first test input fail? When the "hello" is entered it is only to return "l" but with my program it returns "llo". Below is my code and the test results.

    My Code

    public*class*MiddleTest
    {
    ***/**
    ******Gets*the*middle*character*or*character*pair* from*this*string*
    ******when*possible.
    ******@param*str*a*string
    ******@return*the*middle*character*(if*the*string* length*is*odd)*or
    ******the*middle*two*characters*(if*it*is*even),*o r*the*empty*string*if*str*is
    ******empty.
    ****/
    ***public*static*String*getMiddle(String*str)
    ***{
    ****
    *****if*((str.length()*%*2)*==*0)
    *{
    *//Even*length
    **if*(str.length()*>*2)
    ***{
    *return*str.substring(*str.length()*/*2*-*1,*str.length()*/*2*+1);
    ***}
    *}
    *//Odd*length
    *return*str.substring(str.length()*/*2);
    ***}
    }

    Test Results

    Parameters Actual Return Expected Return Outcome
    hello llo l fail
    help el el pass
    l l l pass
    pass


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Trouble with getting middle characters from a string.

    Please edit your post and remove the *s and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    Read the API doc for the String class's substring method to see how to use it to return what you want.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with getting the middle characters of a string!

    I am having a problem getting my first test result to work (which you can see below). When the string is "hello" it returns "llo" instead of "l". What is wrong in my code that is causing this to happen? My code and test results are as follows:

    My code:

     
    public class MiddleTest
    {
       /**
          Gets the middle character or character pair from this string 
          when possible.
          @param str a string
          @return the middle character (if the string length is odd) or
          the middle two characters (if it is even), or the empty string if str is
          empty.
       */
       public static String getMiddle(String str)
       {
     
         if ((str.length() % 2) == 0)
    	 {
    	 //Even length
    	  if (str.length() > 2)
    	   {
    	 return str.substring( str.length() / 2 - 1, str.length() / 2 +1);
    	   }
    	 }
    	 //Odd length
    	 return str.substring(str.length() / 2);
       }
    }

    Test Results

    Test Results

    Parameters Actual Expected Outcome
    hello llo l fail
    help el el pass
    l l l pass
    pass

Similar Threads

  1. Replies: 4
    Last Post: February 24th, 2013, 08:29 PM
  2. Split a string into individual characters
    By djl1990 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 6th, 2012, 03:55 PM
  3. Get characters between specific elements in string
    By hacikho in forum Java Theory & Questions
    Replies: 1
    Last Post: November 23rd, 2011, 07:51 PM
  4. Finding frequency and probability of characters in a string
    By Aberforth in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 31st, 2010, 02:02 AM
  5. How to check whether the string contains only the specified characters ????
    By j_kathiresan in forum Java Theory & Questions
    Replies: 3
    Last Post: April 30th, 2010, 08:49 AM