How to set text (in label) from the textfield [duplicate] - java

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 3 years ago.
I'm making a cash register.
I would like to set the text of the label (cost) when the text in the textfield (receipt) is "chocolate".
private void dodaj_produktActionPerformed(java.awt.event.ActionEvent evt)
{
product_name.getText();
String str = product_name.getText();
receipt.setText(str);
String chocolate="chocolate";
if(str == chocolate){
Double cena=3.50;
String cena_tabliczka = Double.toString(cena);
cost.setText(cena_tabliczka);
}
}

You are comparing incorrectly. In Java you need to use the .equals() operator for comparing objects.
Instead of:
if(str == chocolate){
Try this:
if(str.equals(chocolate)){
Or if you don't care about case
if(str.equalsIgnoreCase(chocolate)){

Related

String concatenation showing unexpected behavior [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 2 years ago.
Bellow is snippet of my code. I just want to know why if condition is not being executed even if variable "ans" has value 2020.
private static void solve() throws Exception {
String str="20";
String str1="20";
String ans=str+str1;
if(ans=="2020")
System.out.println("matched");
else System.out.println("Not matched");
System.out.println(ans);
}
ans.equals("2020")
this should work.

how to check if a string is empty or null in Android Studio [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 6 years ago.
In my project I have an edit text field and its name is editText also I have assign mTextEmri = (EditText) findViewById(R.id.editText); I want to get its value and I have done it with the following statment String emridhene = mTextEmri.getText().toString();
Now I want to check it if it is null or empty I have tried this code but it is not working
if (emridhene.toString() == null || emridhene.toString() == "") {
mTextViewKonfirm.setText("Ju lutem shkruani nje Emer");
}
else{
mTextViewKonfirm.setText(emridhene);
}
How can I make it to work. Thanks.
Just Use
if(TextUtils.isEmpty(/* your String*/)){
// String is empty or null
}else {
// string has value
}

Comparing Strings not Working in Java [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
I'm importing a string from a file and the string is "Computer_Made". If I execute this code, though, it does not print "The computer is already made!" Any ideas?
if (data=="Computer_Made")
{
computer=true;
System.out.println("The computer is already made!");
}
You should use .equals for string comparison!!
if (data.equals("Computer_Made"))
{
computer=true;
System.out.println("The computer is already made!");
}
Refer here for more info
In Java, String are compared using equals or equalsIgnoreCase method.
Using == is reference equality and will rarely be the same.
Try instead:
if (data.equals("Computer_Made"))
== will only work in example like this:
String a = "Ha";
String b = a;
System.out.println("a==b :" + a==b); //prints a==b : true

decision making statement with string condition [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
What is the correct way to put string in a condition statement?
I tried this, but it won't print x.
String c = "c" ,d = "d";
Scanner in = new Scanner(System.in);
String selection = in.next();
if ( c == selection){
System.out.println("x");
Assume I input c.
Strings in Java are compared using str.equals(<string to compare>);
So in your case it would be if(c.equals(selection)) not if(c==selection)

If comparison in Java miss [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I have a portion of code that should do that:
1) Take the name of a color from keyboard
2) Give that color to an object in wich i have another object declaration
3) Decrement from 2 to 1 the correspondant line of the array of 2.
In the main class i want to memorize the color in a variable TemporaryColor.
Scanner input = new Scanner(System.in);
String TemporaryColor = input.nextLine();
playerOrd[1].DecrementoSegnalino(TemporaryColor);
playerOrd is a class that contains this method:
public void DecrementoSegnalino(String color) {
SegnalinoScommessaGiocatore.decrementaSegnaliniScommessa(color);
}
SegnalinoScommessaGiocatore have this array:
private int[] numeroSegnaliniScommessa = {2,2,2,2,2,2};
and this method:
public void decrementaSegnaliniScommessa(String color) {
if (color.equalsIgnoreCase("Black") numeroSegnaliniScommessa[0]--;
if (color.equalsIgnoreCase("Blue") numeroSegnaliniScommessa[1]--;
if (color.equalsIgnoreCase("Green") numeroSegnaliniScommessa[2]--;
if (color.qualsIgnoreCase("Red") numeroSegnaliniScommessa[3]--;
if (color.equalsIgnoreCase("Yellow") numeroSegnaliniScommessa[4]--;
if (color.equalsIgnoreCase("White") ) numeroSegnaliniScommessa[5]--;
}
There is a problem passing the string that i write with Keyboard...
If i use this in the beginning:
playerOrd[1].DecrementoSegnalino("Black");
It works!
What's the problem?
Don't compare Strings using ==. Use the equals(...) or the equalsIgnoreCase(...) method instead. Understand that == checks if the two objects are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. So instead of
if (fu == "bar") {
// do something
}
do,
if ("bar".equals(fu)) {
// do something
}
or,
if ("bar".equalsIgnoreCase(fu)) {
// do something
}

Categories

Resources