• Log in or sign up for an account here: https://www.sythe.org/login

Changing a String

Joined
Oct 17, 2009
Sythe Gold
0
Messages
873
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
 
You should compare Strings using equals() rather then ==, as == can yield unpredictable results at times.
 
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!
 
Supporting Business
Discord Voice
Poképedia
Who did this to my freakin' car!
Dat Boi
Dolan Duck
Dolan Trump
I'm LAAAAAAAME
Live Free or Die
Mew
Mewtwo
PHP Shit Show 2k26
Poké Prizebox
Rust Player
Sythe Awards 2016 Winner
Sythe Awards 2017 Winner
Sythe Awards 2019 Winner
SythePro Subscriber
Tier 3 Prizebox
Toast Wallet User
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
 
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!

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

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).
 
Is it possible to use StringBuilder to display a string that the users input, backwards?
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).
 
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.
 
duelarena.gg
Back
Top