Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By Rom 88

Thread: Replacing characters at certain positions in strings

  1. #1
    Rom 88 is offline Member
    Join Date
    May 2012
    Posts
    6
    Rep Power
    0

    Question Replacing characters at certain positions in strings

    Hi guys, first time on the forum here and I have just spent about 10 minutes searching through these forums looking for an answer to my question, but I can't find one.

    It's quite basic, but here is my problem. I need to simulate a race (I've seen similar threads on this but not with my question), and the racetrack is basically a string of ------ (65 chars long).
    There are two animals racing, a hare and a tortoise. I have programmed most of it but I'm unsure on how to proceed with displaying the positions of each animal on the racetrack. For example, if the tortoise is at position 50, there should be a "T" at the 50th position on the racetrack. If it then moves up by 5 spots, the "T" should appear at position 55, and a "-" should reappear at position 50.

    At first I thought about swapping two tiles, but I don't think that's possible without the use of arrays (which I'm not allowed using).

    So I'm thinking that I should basically just reset the track to all "----" after each position move and then simply add the "H" or "T" to the new position. Only problem is that I'm not sure how to do that.

    In basic English, I'd like to: "Replace character at position "50" (or wtv else position) with an "H" or a "T"."

    Any help would be greatly appreciated!
    Last edited by Rom 88; 05-29-2012 at 11:57 PM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Replacing characters at certain positions in strings

    Since Strings are immutable, you will have to build a new String from the current String. Can you use the StringBuilder or StringBuffer classes? Otherwise use the methods of the String class to get the unchanged parts of the String and use them to build the new String.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Rom 88 is offline Member
    Join Date
    May 2012
    Posts
    6
    Rep Power
    0

    Default Re: Replacing characters at certain positions in strings

    Thanks for your reply! I had actually gone in and dug into a java book I had and was able to find the StringBuilder and StringBuffer classes, and used the "getCharAt" method. To be honest, I'm not sure if I'm allowed to use them since we didn't actually learn about them, but at this point my program is almost done so it'll have to do.
    Fubarable likes this.

Similar Threads

  1. Replacing number characters with blanks
    By rpark712 in forum New To Java
    Replies: 3
    Last Post: 01-26-2012, 04:15 PM
  2. Replacing characters at a certain index
    By Slayer in forum New To Java
    Replies: 2
    Last Post: 03-10-2011, 10:49 PM
  3. Searching and replacing characters in a File
    By DBaskov in forum New To Java
    Replies: 3
    Last Post: 02-01-2011, 12:39 AM
  4. replacing characters???
    By manda147 in forum New To Java
    Replies: 2
    Last Post: 11-29-2008, 09:19 AM
  5. characters + strings
    By Gilgamesh in forum New To Java
    Replies: 3
    Last Post: 03-02-2008, 10:10 PM

Tags for this Thread

Posting Permissions

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