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: .indexOf selective replacements

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default .indexOf selective replacements

    I have a StringBuffer named "sentence." I need to replace all occurrences of "A", except occurrences of "A" at the end of a word, with "E".

    Example: ABABA ---> EBEBA

    However, my code returns EBEBE.

    while (sentence.indexOf("A") >= 0 && sentence.indexOf("A") != sentence.indexOf("A ") && sentence.indexOf("A") != sentence.indexOf("A."))
    		{
    			int AStart = sentence.indexOf("A");
    			int AEnd = AStart + "A".length();
    			sentence.replace(AStart, AEnd, "E");
     
    		}


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,936
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: .indexOf selective replacements

    Is the problem that the code replaces the last "A" with an "E"?

    Add an if test to see where the location is and don't replace if it is at the end.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,699
    Thanks
    25
    Thanked 334 Times in 310 Posts

    Default Re: .indexOf selective replacements

    Well, why not just check if sentence.indexOf("A")==sentence.length()-1?

    That statement says: if the index of A is the last index

    The reason it says that is because sentence.length() returns the length of the StringBuffer, while sentence.length()-1 returns the last index of the StringBuffer.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. Replies: 7
    Last Post: December 11th, 2011, 11:58 PM
  2. newby Questions - indexOf help with an ArrayList
    By Bially in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 20th, 2011, 07:05 PM
  3. contains vs. indexOf
    By mgrootsch in forum Java Theory & Questions
    Replies: 5
    Last Post: September 17th, 2009, 09:09 AM