This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
Consider my code below:
System.out.println("Insert your inventory");
for (int i = 0; i<20;i++) {
System.out.print(i+1+".");
if (inventory[i] == "N" || inventory[i]=="n") {
break;
}
inventory[i] = s.nextLine();
}
How can I exit from this loop if the user enters 'N' or 'n'?
You're comparing string with == operator. It does not yield correct result because your constant string "N" and your input "N" do not have same reference/pointer.
You have to use equals() to guarantee the correct compare result between strings.
Replace
if (inventory[i] == "N" || inventory[i]=="n")
With
if (inventory[i].equals("N") || inventory[i].equals("n"))
You should compare your String variables with the .equals() method instead of the == operator.
An explanation about why this is important can be found here on StackOverflow.
Related
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
I am not sure why but when I get a string from the user, I cannot compare it in an if statement but when I try to print it, it works fine.
Part of my code:
Scanner in = new Scanner(System.in);
while (true) {
String userInput;
int rowInput, colInput;
printBoard(board);
System.out.print("Move: ");
userInput = in.next();
// shift board right on a row
if (userInput == "r") {
System.out.print("row #: \r");
rowInput = in.nextInt();
moveRight(--rowInput, board);
}
Does anyone know why this isn't working as expected?
You an try this:
if (userInput.equals("r"))
== is used to compare the address and equals is used to compare contents.
I should be using equals instead of ==.
So it would lead to:
...
if (userInput.equals("r"))
...
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
Noob Java question: Why won't this Do While loop accept the user input? When I use a different variation (such as int for the answer), it works. But when I look for a string, it never accepts the string and escapes the loop.
This works:
int value = 0;
do {
System.out.println("Enter a number: ");
value = scanner.nextInt();
}
while(value != 5);
System.out.println("Do while loop has ended.");
This doesn't work:
String pass;
String word = "word";
do {
System.out.println("Enter password: ");
pass = scanner.nextLine();
}
while(pass != word);
System.out.println("Password accepted.");
Thanks
Change this:
while(pass != word);
to this:
while(!pass.equals(word));
You were comparing the references when you used !=, not the actual content of the strings. Since they did not point to the same String, your loop would always exit on the first run.
"==" compares addresses in memory so if you enter the word which will be the same, the reference you have stored will point to different object.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
What is the difference between == and equals() in Java?
(26 answers)
Closed 8 years ago.
I have the following code:
public void convert()
{
String val = (String) spinner1.getItemAtPosition(spinner1.getSelectedItemPosition());
System.out.println(spinner1.getItemAtPosition(spinner1.getSelectedItemPosition()));
System.out.println(val);
if(val == "mm" || val == "cm" || val == "m" || val == "km")
{
System.out.println("got into if statements");
initiateLengthConvert();
}
System.out.println("never got it");
}
Now when the first to print statements print out, if I selected "mm", then it prints out:
mm
mm
never got it
Why won't it pass into the if statement? "val" and the possibilities match up, so it doesn't make any sense to me.
Use .equals to compare strings instead of ==.
if(val.equals("mm") || val.equals("cm") || val.equals("m") || val.equals("km"))
Read
What is the difference between == vs equals() in Java?
Also instead of System.out.println("..."); use Log
Try this..
== always just compares two references. String you should use .equals("")
if(val.equals("mm") || val.equals("cm") || val.equals("m") || val.equals("km"))
{
System.out.println("got into if statements");
initiateLengthConvert();
}
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
How do I know if String letter is equal to char array
String[] choose = {"a","d","t","b","s","f","x"};
String check;
boolean error = false;
System.out.print("Enter");
Scanner sn = new Scanner(System.in);
check = sn.nextLine();
for (int i = 0 ; i < choose.length;i++){
if(choose[i] == check){
System.out.print("you entered" + choose[i]);
break;
}
}
What I did is this it didnt confirm I input letter a but "you entered" didnt show up.
You cannot test strings for equality using ==. That only compares references (memory addresses). You need to use String#equals(Object). In general == is most certainly what you don't want if you are testing for equality, unless you are checking to see if two variables are pointing to the same instance. This is rarely the case, since you are usually interested in testing values for equality.
So what you need to do is:
if(choose[i].equals(check)) {
...
}
You are trying to compare strings with ==, which only compares the references, but not the values. What you want to use is
if(check.equals(choose[i]))
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
String is not equal to string?
What makes reference comparison (==) work for some strings in Java?
can some one explain me following java code
String a = "1";
if(a == "1") {
//print compare 1 is true;
} else {
//print compare 1 is false;
}
if(a.equals("1")) {
//print compare 2 is true;
} else {
//print compare 2 is false;
}
it results like
compare 1 is false
compare 2 is true
Only explanation i have is that its comparing the memory address not the values itself. But i am not sure. can some please put a light on it. in .Net == operator is overloaded to compare contents of string.
use "1".equals(a) , String is an object so use equals() to compare
I understood that == operator is compare "Is it same object?"
object a is not same object with constant string "1".
so returns false