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

Changing a String

DtheK

Apprentice

Banned
Joined
Oct 17, 2009
Sythe Gold
0
Messages
873
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
 
Changing a String
You should compare Strings using equals() rather then ==, as == can yield unpredictable results at times.
 
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!
 
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!

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

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).
 
Changing a String
Changing a String
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).
 
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.
 
Runescape Gamblingrunescape gambling
Back
Top