Results 1 to 5 of 5
  1. #1
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default Program works but don't exactly understand it

    Hey guys,

    I wrote the below program and it works fine but my question is. Why I didn't have to put d = s.substring(i , j - 1); but had to put d = s.substring(i , j); to make it work. In the first while loop i used the i + 1. Can't figure that out.

    My program is posted below....

    String trim(String s){
    /* String trim(String s) that creates and returns a string that is otherwise the same as the parameter string,
    but all whitespace in the beginning and end of the string is removed. The whitespace inside the string between
    words is untouched. For example, when called with the string “ test pattern ”, the method would return the string
    “test pattern”. If the string contains nothing but whitespace, the method should return the empty string.
    (The class String already has a method trim that behaves this way, but of course you are not allowed to use it,
    but you have to write your own.) */

    int i = 0; //beginning of string
    int j = s.length() -1; //end of string
    String d = ""; //return string
    while(s.charAt(i) == ' ')
    {
    d = s.substring(i + 1);
    i++;
    }
    while (s.charAt(j) == ' ')
    {
    d = s.substring(i , j);
    j--;
    }
    return d;
    }
    }

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Work it out on paper. Remember that i starts at the very left of the string, and j starts at the very right. Therefore, i is BEFORE its target character, and j is after. So, you have to add to i to skip over the character, whereas j you do not.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Did you read the substring() API docs?

  4. #4
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default

    oh, i get it, thanks alot for the help, couldn't figure this out on my own.

    Thought i took care of the -1 by putting in the length but didn't poperly read the API

    OK. get it now......you guys are helping me alot with this stuff.

    Java can be so frustrating.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    The -1 in the length takes care of something else: a 5 element array (length, 5) goes up to index 4. 0, 1, 2, 3, 4

    You get used to the fact that the range is inclusive on the left and exclusive on the right. It happens all over the place including the "normal" way of writing a for loop

    Java Code:
    for(int ndx = 0; ndx < limit; ndx++)

    This will loop over limit things with the index including 0 but stopping before it gets to ndx.

Similar Threads

  1. Trying to understand
    By ladykrimson in forum New To Java
    Replies: 20
    Last Post: 10-13-2010, 12:10 AM
  2. Replies: 3
    Last Post: 07-28-2010, 03:49 PM
  3. Replies: 7
    Last Post: 03-31-2009, 02:29 PM
  4. I don´t understand
    By Manikyr in forum New To Java
    Replies: 6
    Last Post: 02-23-2009, 12:22 AM
  5. Replies: 2
    Last Post: 11-18-2008, 11:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •