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: Beginner Question length method

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Beginner Question length method

    Given a string, return a version where all the "x" have been removed. Except an "x" at the very start or end should not be removed.

    public String stringX(String str) {
    String a = "";
    for (int i = 0; str.length()>i; i++) {
    if (!(i > 0 && i < (str.length()-1) && str.substring(i,i+1).equals("x")))
    a = a + str.charAt(i);
     
    } 
     
    return a; 
    }

    Question is in regards to the process that this statement is fulfilling for length. i < (str.length()-1) I don't understand why, without this condition, it wont check for an x in the very last variable which will return the string without the final x character if there is one.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Beginner Question length method

    Write it out on paper using an example. If the input string is of length 3, it's last character is at position 2. What would the final character be if the termination condition of the loop is (as written) i < (length() - 1)?

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Beginner Question length method

    Another way to look at the problem: the first and last characters are always copied, only intervening 'x's are removed.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: March 6th, 2014, 06:29 PM
  2. Replies: 9
    Last Post: February 26th, 2013, 11:36 AM
  3. Weird String .length() method
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 11th, 2012, 10:30 AM
  4. text.length method problem?
    By computercoder in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 3rd, 2010, 10:40 AM
  5. Comparing Strings only using the .length() method - possible?
    By Ryker in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 16th, 2010, 05:52 PM