Results 1 to 11 of 11
  1. #1
    MonkeyMan is offline Member
    Join Date
    Jan 2012
    Posts
    45
    Rep Power
    0

    Default Stringbuilder hogging up the string >:(

    Heya!

    Simple enough quickie of a question, why does this not print out the new string I built using the stringbuilder?

    display text is, say, 12.1

    Java Code:
      
    
                String s = display.getText();
    
                StringBuilder builder = new StringBuilder(s);
    
                int index = builder.indexOf(".");
                int sLength = s.length();
    
                sLength = s.length() - s.indexOf('.');
    
                builder = builder.delete(index, sLength);
    
                s = builder.toString();
    
                display.setText(s);

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,017
    Rep Power
    10

    Default Re: Stringbuilder hogging up the string >:(

    I recommend you step through this with a debugger, or at least add a print statement every other line, so you can better understand what's going on here.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    MonkeyMan is offline Member
    Join Date
    Jan 2012
    Posts
    45
    Rep Power
    0

    Default Re: Stringbuilder hogging up the string >:(

    Whops I see the logic error in the builder statement, however, it doesn't explain why it doesn't print the crap it does o.o

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,017
    Rep Power
    10

    Default Re: Stringbuilder hogging up the string >:(

    Quote Originally Posted by MonkeyMan View Post
    Whops I see the logic error in the builder statement, however, it doesn't explain why it doesn't print the crap it does o.o
    Well, we don't know how you're calling this, or what it outputs, so we can't really help you. If you want us to try, I suggest you let us know exactly what it's doing and throw together an SSCCE we can run to see for ourselves.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    MonkeyMan is offline Member
    Join Date
    Jan 2012
    Posts
    45
    Rep Power
    0

    Default Re: Stringbuilder hogging up the string >:(

    Got it fixed. All I had to do is fix the logic meaning I shouldn't try making the builder delete from index 4 to index 1 :)

    Tho I wouldn't have been able to give you more code since that's pretty much all working code there is, cept the default declaration of the main method. It's simply getting text off a textfield and then doing what you see to it, and putting the edited text back to the textfield :)

    edit: oh right, forgot I didn't add that it's a GUI button, actionperformed, sorry! :)
    Last edited by MonkeyMan; 01-19-2012 at 05:47 PM.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,183
    Rep Power
    20

    Default Re: Stringbuilder hogging up the string >:(

    You are either not telling us the exception you're getting (delete() throws an array index exception if end is less than start), or you are eating exceptions.

  7. #7
    MonkeyMan is offline Member
    Join Date
    Jan 2012
    Posts
    45
    Rep Power
    0

    Default Re: Stringbuilder hogging up the string >:(

    I wasn't getting an exception at all :)

    It was refusing to display the new string from stringbuilder, however it was fixed when I stopped telling it to delete from index 3 to index 1, since it can't really delete backwards :D

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,183
    Rep Power
    20

    Default Re: Stringbuilder hogging up the string >:(

    No.
    That is impossible.
    As the API says:
    "
    StringIndexOutOfBoundsException - if start is negative, greater than length(), or greater than end.
    "
    So you must be eating exceptions in your code.

  9. #9
    MonkeyMan is offline Member
    Join Date
    Jan 2012
    Posts
    45
    Rep Power
    0

    Default Re: Stringbuilder hogging up the string >:(

    Well, I dunno what to tell ya. Maybe I'm sleep eating?

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: Stringbuilder hogging up the string >:(

    Rather than try to be funny, you could benefit by posting an SSCCE for review, as Kevin suggested.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,183
    Rep Power
    20

    Default Re: Stringbuilder hogging up the string >:(

    This is eating exceptions:
    Java Code:
    try {
    ...some code that throws exceptions
    } catch (Exception ex) {
    // Do nothing in here which means I have no idea when an exception is thrown
    }

Similar Threads

  1. String vs. StringBuilder vs. StringBuffer
    By stchman in forum New To Java
    Replies: 5
    Last Post: 01-12-2012, 08:52 AM
  2. String concatenation vs. StringBuilder.
    By stchman in forum New To Java
    Replies: 5
    Last Post: 08-23-2011, 12:17 PM
  3. StringBuilder
    By Dayanand in forum New To Java
    Replies: 5
    Last Post: 03-04-2011, 09:31 AM
  4. StringBuilder
    By tmotse in forum New To Java
    Replies: 3
    Last Post: 09-13-2010, 09:07 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
  •