kullalok
kullalok

Reputation: 823

How to clear a string?

In a program I'm working on, a textfield has to display some text at some point.

output.setText( outputString );
outputString = "";

output is a JTextField. These lines of code are in a method, and when it is called the first time, it works perfectly fine. However, when it is called another time, the original outputString text still remains. Why does this happen, and how can I fix it?

Okay, I think it happens because strings are immutable. The thing is, outputString never changes, so it still has the text from the initial method call.

How do I, somehow, change the text in the string?

Upvotes: 8

Views: 86241

Answers (10)

cellepo
cellepo

Reputation: 4479

Other answers pointing to the fact that Strings are immutable are accurate.

But if you want to have the functionality of "clearing a String", you can use a StringBuffer instead and call this on it:

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

Upvotes: 0

Djkgotso
Djkgotso

Reputation: 130

this will work out just fine.

output.setString("");

Upvotes: 0

Stephen C
Stephen C

Reputation: 718688

I think you are misunderstanding something very important. Java Strings are immutable that means that you can't change them.

"How do I, somehow, change the text in the string?"

You can't. The text in the string cannot be changed. It is immutable. Any "solution" that involves changing the text in a String WON'T WORK in Java. (Got that?)

When you do this:

output.setText(outputString);
outputString = "";

the assignment does not change the value that is displayed in the text field. It just changes the String that the local variable outputString refers to.

And when you do this:

output.setText(""); 
output.setText(outputString);

it does not cause outputString to change. It just changes the displayed text to nothing and then immediately changes it to whatever outputString currently refers to.

If you want to change the value displayed in the text field to nothing, you JUST do this:

output.setText("");

Perhaps the other thing that you've got wrong in your thinking is that you think that this:

output.setText(outputString);

sets up a relationship between the text field output and the variable outputString ... such that when the user types into the field, the outputString variable is magically updated. That is NOT so. In fact, it CANNOT be so, because you cannot pass the address of variable.

In fact, output.setText(outputString); just passes the value of outputString to the text box object. If and when the user types something into the box, the characters are stored somewhere else, and only returned to your code ... as a new String ... when your code calls output.getText().

Upvotes: 2

Chris Dargis
Chris Dargis

Reputation: 6043

You have to clear the text from the JTextField object. Sending a variable as an argument with setText() does not tie that variable to the object. It simply copies the string.

output.setText("");

If the text is null or empty, has the effect of simply deleting the old text.

http://docs.oracle.com/javase/6/docs/api/javax/swing/text/JTextComponent.html#setText%28java.lang.String%29

Upvotes: 4

DP Greveling
DP Greveling

Reputation: 409

I think what you are looking for is:

 output.setText("");

Upvotes: 0

Kallja
Kallja

Reputation: 5472

What your code is doing, is first setting the contents of the text field to the contents of your outputString variable in: output.setText( outputString );.

Your subsequent code line does not change the String object, which is the content of your text field, but rather set your outputString variable to refer to the empty string variable. Strings being immutable in java, it is impossible to have changes to one string variable reflect to an other.

As a number of people have stated, you need to set your text fields contents by calling: jTextFieldVar.setText(""); or jTextFieldVar.setText(null);.

Upvotes: 1

d1e
d1e

Reputation: 6442

Why does this happen?

String are immutable.

There is always a new reference to the String. Thus, when you call outputString = "";, the new String is created.

How can I fix it?

Call output.setText("");.

Upvotes: 1

Alpesh Prajapati
Alpesh Prajapati

Reputation: 1643

output.setText("");

Will solve this problem

Upvotes: 1

Logard
Logard

Reputation: 1513

Are you setting the outputstring to "" after you try to set the JTextField's text? If so you should try to do something like this:

  output.setText("");

Upvotes: 0

Theodore Murdock
Theodore Murdock

Reputation: 1580

Setting the text to the contents of your variable does not set up a permanent relationship between that variable and that text field, if you want to clear the text, you could use

output.setText("");

Upvotes: 5

Related Questions