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 7 of 7

Thread: clearing StringBuilder

  1. #1
    Member
    Join Date
    Jul 2013
    Location
    Baltimore, MD
    Posts
    59
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default clearing StringBuilder

    I'm using StringBuilder since I don't know how many times something could be added to a string.

    Is there a way to clear out the StringBuilder? As you can see below, the first time the program runs it works fine, but if I decide to play again, it adds old string to new.

    Welcome to Thirty3
     
    Would you like to play (Y/Q)? y
     
    Your cards: four of Hearts, Eight of Spades, Seven of Spades,  (Total 19)
    Would you like another card (Y/N)? y
    Your cards:four of Hearts, Eight of Spades, Seven of Spades, Six of Diamonds,  (Total 25)
    Would you like another card (Y/N)? y
    Your cards:four of Hearts, Eight of Spades, Seven of Spades, Six of Diamonds, Eight of Spades,  (Total 33)
    Would you like another card (Y/N)? n
     
    The dealer drew: Seven of Spades, Three of Diamonds, Six of Hearts, Eight of Hearts, Queen of Hearts,  (Total 34)
     
    You  - Current Total - Wins - 0 Ties - 0 Loses - 0
     
    Would you like to play again (Y/Q)? y
     
    Your cards: Ten of Spades, four of Spades, Five of Hearts,  (Total 19)
    Would you like another card (Y/N)? y
     
    // this is where the problem is, it should be Ten of Spades, four of Spades, Five of Hearts, then something else
     
    Your cards:four of Hearts, Eight of Spades, Seven of Spades, Ten of Spades, four of Spades, Five of Hearts, Six of Diamonds, Eight of Spades, Five of Hearts,  (Total 24)
    Would you like another card (Y/N)? n
     
    The dealer drew: Ace of Hearts, Three of Spades, Ten of Spades, Three of Hearts,  (Total 27)
     
    You  - Current Total - Wins - 0 Ties - 0 Loses - 0
     
    Would you like to play again (Y/Q)? n


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: clearing StringBuilder

    I'm using StringBuilder since I don't know how many times something could be added to a string.
    Interesting. Technically, String objects are immutable, so "changing" one actually creates a new one unless the new version already exists in the String pool.
    Is there a way to clear out the StringBuilder?
    Reinitialize it empty.

  3. #3
    Member
    Join Date
    Jul 2013
    Location
    Baltimore, MD
    Posts
    59
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: clearing StringBuilder

    so far, I've tried

    StringBuilder.setLength(0);

    But it still adds on to the old string

    --- Update ---

    I sent you a PM

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: clearing StringBuilder

    // reinitialize an existing StringBuilder object empty
    stringBuilder = new StringBuilder();

  5. #5
    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: clearing StringBuilder

    Is there a way to clear out the StringBuilder?
    ...or delete the contents of the StringBuilder, starting at index 0 and going up to its length

  6. #6
    Member
    Join Date
    Jul 2013
    Location
    Baltimore, MD
    Posts
    59
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: clearing StringBuilder

    I tried

    new StringBuilder(), not working
    StringBuilder.replace(), also not working

    --- Update ---

    Quote Originally Posted by GregBrannon View Post
    // reinitialize an existing StringBuilder object empty
    stringBuilder = new StringBuilder();
    Not working for me for some reason!

    --- Update ---

    Quote Originally Posted by copeg View Post
    ...or delete the contents of the StringBuilder, starting at index 0 and going up to its length
    I tried that also, not working

    StringBuilder.delete(0, StringBuilder.length());

    --- Update ---

    Here's the part of the code I think where the problem is, I want everything to reset to start a new "game"

                if(playAgain.equalsIgnoreCase("y"))
                {
                    playerTotal = 0;
                    dealerTotal = 0;
                    playerPoints = 0;
                    dealerPoints = 0;
     
                    PSB.delete(0, PSB.length());
                }


    --- Update ---

    I've got it. I forgot to "clear" my for loop String. I made it to " " at the end.

    Thanks,

    Tony

  7. #7
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: clearing StringBuilder

    "not working" provides zero information. Do you get an error? then copy and paste the EXACT error message. Do you get incorrect behaviour? Then describe what does happen and what should happen instead.
    Improving the world one idiot at a time!

Similar Threads

  1. StringBuilder Vs StringBuffer
    By qazi in forum Java Theory & Questions
    Replies: 0
    Last Post: July 28th, 2013, 08:31 AM
  2. StringBuilder
    By Anoop Shiralige in forum Java Theory & Questions
    Replies: 1
    Last Post: June 23rd, 2012, 07:08 AM
  3. Clearing the screen
    By ranjithfs1 in forum Java Theory & Questions
    Replies: 4
    Last Post: March 19th, 2012, 10:04 AM
  4. Help with clearing variables
    By fmr in forum Other Programming Languages
    Replies: 2
    Last Post: July 18th, 2011, 10:18 AM
  5. [SOLVED] New line and StringBuilder
    By newbie in forum What's Wrong With My Code?
    Replies: 9
    Last Post: January 1st, 2011, 09:42 PM