Results 1 to 2 of 2
  1. #1
    GGarriga is offline Member
    Join Date
    Feb 2017
    Rep Power

    Default Wrong answer with string

    Hi, I'm a student that is learning Java, and I have this code:
    Java Code:
    lletres = lletres.replace(lletres.charAt(2), codi.charAt(codi.indexOf(lletres.charAt(2)) + 1));
    lletres is a string, and it's like this
    Java Code:
    lletres = "BBB"
    The result is "CCC" and I only want to change the las B, so the result can be like this: "BBC".
    Thanks so much!

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Rep Power

    Default Re: Wrong answer with string

    Well, the API docs for replace state:
    Returns a string resulting from replacing all occurrences of oldChar in this string with newChar.
    So that's not the method you want.

    You can get the lastIndexOf 'B', and get the char[] (toCharArray), change the index you want, then create a String from that char[]? Since there isn't a replaceLast method.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Why my answer is wrong
    By vknehra10 in forum New To Java
    Replies: 1
    Last Post: 12-26-2016, 05:32 PM
  2. Replies: 17
    Last Post: 09-15-2012, 03:50 AM
  3. Replies: 1
    Last Post: 08-30-2012, 12:07 AM
  4. Compare true Answer /Player answer
    By Eilime in forum New To Java
    Replies: 1
    Last Post: 04-24-2011, 08:51 PM
  5. Replies: 3
    Last Post: 02-27-2011, 12:03 AM

Posting Permissions

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