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.
Related
This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Can I pass parameters by reference in Java?
(8 answers)
How can I simulate pass by reference in Java?
(8 answers)
Closed 9 months ago.
Can I return or change the value of a string variable when called from Java? I know that a method can return a value, but if I want to have some values changed, can I use a pointer or something?
For example, if I would be writing a method to extract the first characters of a String, and leave the original string without those values, could I do something like:
public static String popFirstChars(String text, int amount) {
String extract = text.substring(0, amount);
String remaining = text.substring(amount +1, text.length());
// this is what I don't know if it's possible:
text = remaining;
// this is the standard return
return extract;
}
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)){
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 6 years ago.
i got a little problem with my code. When i write "exit" still shows statement "Unknown command" and i want to show "Bye". Can you help me?
import java.util.Scanner;
public class Hello{
public static void main(String[] args){
Scanner odczyt = new Scanner(System.in);
String word;
do{
word = odczyt.nextLine();
System.out.println("Unknown command");
}
while(word!="exit");
System.out.println("Bye");
}
}
"Unknown command" will always be printed. Besides that you shouldn't use = to compare Strings. You should use .equals() or .equalsIgnoreCase().
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I want to ask a question.
I feel so confused about my own coding because i think it is correct.
This is the issue.
public static void main(String[] args) {
String x = "Robert : Hi There";
String y = "Robert";
System.out.println(x.substring(0, x.indexOf(":")).trim());
if(x.substring(0, x.indexOf(":")).trim() != y){
System.out.println("Pass");
}
else
{
System.out.println("Not Pass");
}
}
This gave me output:
Robert
Pass
I want the output is "Not Pass" but why did my coding gave another result.
I hope you can tell what is wrong.
Thank You.
You compare string objects. So you have to ue the equals method:
if(x.substring(0, x.indexOf(":")).trim().equls(y)){
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I'm not experienced in Java, and I have a problem.
Using Jsoup, I have an element called td. If I do:
String attr = td.attr("class");
System.out.println(attr);
The output is "free", which is perfectly alright. If I do:
String attr = td.attr("class");
if (attr == "free") {
System.out.println("freedom!");
}
There is no output!
Does anyone know how to solve this problem?
Thanks in advance.
You have to compare the string using the equals method as == compare references, not strings contents.
String attr = td.attr("class");
if (attr.equals("free")) {
System.out.println("freedom!");
}