Changing a String

Discussion in 'Programming General' started by DtheK, Feb 20, 2010.

Changing a String
  1. Unread #1 - Feb 20, 2010 at 5:07 PM
  2. DtheK
    Joined:
    Oct 17, 2009
    Posts:
    873
    Referrals:
    0
    Sythe Gold:
    0

    DtheK Apprentice
    Banned

    Changing a String

    Code:
    String s = JOptionPane.showInputDialog("Enter the Input:");
    String c = "";
    int n = s.length();
    String a = s.substring(0,2);
    String b = s.substring(2,n);
    if(a=="HE")
    	c="E";
    		
    		System.out.println(c+b);
    I want the code above to print out ELLO when I input HELLO. It compiles, but when I run it, it just prints out LLO. I tried printing out the string c too, but it doesn't display anything...I know the error must be somewhere here:

    Code:
    if(a=="HE")
    		c="E";
    Sorry if I am asking a dumb question, but I am pretty new to working with this kind of stuff
     
  3. Unread #2 - Feb 20, 2010 at 9:22 PM
  4. Jimmy
    Joined:
    Jun 24, 2008
    Posts:
    2,421
    Referrals:
    10
    Sythe Gold:
    25

    Jimmy Ghost
    Retired Sectional Moderator $5 USD Donor

    Changing a String

    You should compare Strings using equals() rather then ==, as == can yield unpredictable results at times.
     
  5. Unread #3 - Feb 21, 2010 at 1:34 AM
  6. DtheK
    Joined:
    Oct 17, 2009
    Posts:
    873
    Referrals:
    0
    Sythe Gold:
    0

    DtheK Apprentice
    Banned

    Changing a String

    Hmm, I printed out 'a' after assigning it s.substring(0,2) and it gave me HE (when the input was HELLO). Maybe it works as starting=0 and <2.

    I didn't know about the equals(), though. I used that, and it worked. :) Thanks!
     
  7. Unread #4 - Feb 22, 2010 at 7:46 PM
  8. Sythe
    Joined:
    Apr 21, 2005
    Posts:
    8,075
    Referrals:
    468
    Sythe Gold:
    5,290
    Discord Unique ID:
    742989175824842802
    Discord Username:
    Sythe
    Dolan Duck Dolan Trump Supporting Business ???
    Poképedia
    Clefairy Jigglypuff
    Who did this to my freakin' car!
    Hell yeah boooi
    Tier 3 Prizebox Toast Wallet User
    I'm LAAAAAAAME Rust Player Mewtwo Mew Live Free or Die Poké Prizebox (42) Dat Boi

    Sythe Join our discord

    test

    Administrator Village Drunk

    Changing a String

    You can't change a String. It's an immutable class.

    You need to use StringBuilder if you want a mutable string class.

    The compiler will turn 'string addition' into a StringBuilder for you. But it's an inefficient conversion, particularly for string operations done many times a second.

    http://java.sun.com/j2se/1.5.0/docs/api/java/lang/StringBuilder.html
     
  9. Unread #5 - Mar 2, 2010 at 12:25 PM
  10. slashshot007
    Joined:
    May 6, 2006
    Posts:
    164
    Referrals:
    0
    Sythe Gold:
    0

    slashshot007 Active Member

    Changing a String

    you only want to use the == method for primitive variables. since String is not a primitive (thus the capital s), then it you must in almost every case use the .equals() method.

    Example/Explanation: is because string is an object full of chars, the .equals() method compares each char in the method to see if they are the same and in the proper order.

    You will understand more as you begin to create objects, etc.
     
  11. Unread #6 - Mar 2, 2010 at 8:03 PM
  12. Jimmy
    Joined:
    Jun 24, 2008
    Posts:
    2,421
    Referrals:
    10
    Sythe Gold:
    25

    Jimmy Ghost
    Retired Sectional Moderator $5 USD Donor

    Changing a String

    Using the == operator for most non-primitive Objects is fine. Unless (as is the case with a String) the class you want to compare overrides the equals() method, or the class which the Object is an instance of is unknown (meaning that it's possible that it overrides the equals() method), then using == to compare it with another Object is perfectly okay (in java.lang.Object, the equals() method returns that the current instance of java.lang.Object == the instance passed as an argument).
     
  13. Unread #7 - Mar 11, 2010 at 8:55 AM
  14. Darkgroove
    Referrals:
    0

    Darkgroove Guest

    Changing a String

    Is it possible to use StringBuilder to display a string that the users input, backwards?
     
  15. Unread #8 - Mar 11, 2010 at 9:51 AM
  16. Jimmy
    Joined:
    Jun 24, 2008
    Posts:
    2,421
    Referrals:
    10
    Sythe Gold:
    25

    Jimmy Ghost
    Retired Sectional Moderator $5 USD Donor

    Changing a String

    Yes.

    Code:
    String input = ...; //However you get the input
    StringBuilder builder = new StringBuilder(input);
    StringBuilder backwards = builder.reverse();
    String backwardsInput = backwards.toString(); //Convert the StringBuilder back into a java.lang.String
    Of course, unless you need synchronization, it's a better idea to use a StringBuffer (the methods are named the same, however, so all you would do is replace all of the instances of a StringBuilder with that of a StringBuffer).
     
  17. Unread #9 - Mar 12, 2010 at 2:51 AM
  18. Darkgroove
    Referrals:
    0

    Darkgroove Guest

    Changing a String

    You know it's funny, went to my programming lecture today and he basically covered what Sythe said, and I had what Jimmy said in the back of my mind to backup that knowledge. Glad I checked this thread yesterday haha.

    Btw guys the Stringbuffer worked out perfectly, thanks.
     
< I need java help please | Offering programing services >

Users viewing this thread
1 guest


 
 
Adblock breaks this site