This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I'm getting the user input like so:
Scanner keyboard = new Scanner(System.in);
String input = keyboard.nextLine();
When I compare it like this
if(input == "abc"){
System.out.println("Match!");
}else{
System.out.println(input + "\nabc");
}
If I type "abc" I see
abc
abc
Instead of the expected match. What's going on?
You are comparing String references, not String values. Use the String.equals method instead.
== is comparing memory locations. you need to use input.equals("abc") instead.
Related
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 2 years ago.
This is the code I have right now and can't figure out why this while loop isnt ending when there is a period entered.
while (emailTxt!="."){
emailTxt = "";
emailTxt = scanner.nextLine();
totalText.add(emailTxt);
}
emailTxt is a String. A String in Java is considered an Object. You want to use the Object.equals().
So
while(!(emailTxt.equals("."))
You should use equals() to compare two string.
while (!".".equals(emailTxt)) {
// ...
}
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I'm trying to create a program in Java which allows the user to pick from a list of coffees contained in a String array and continues on, but it fails the check and prints out the else statement.
Here is the relevant code:
Scanner scan = new Scanner(System.in);
String[] flavors = {"Black", "French Vanilla", "Hazelnut", "Mango", "Cherokee", "anarcho-syndicalism", "Otis" };
...
System.out.println("Today we have " + Arrays.toString(flavors));
System.out.println("Please enter the name of the coffee you would like exactly as shown above: ");
String coffee = scan.next();
...
for (int i = 0; i < flavors.length; i++) {
if (coffee == flavors[i]) {
String selection = flavors[i];
Though not shown here, I believe everything's properly formatted later on in the program. Any ideas?
In general, when comparing objects for equality in java, use .equals(). Use == for comparing primitives. In java, Strings are objects.
change:
if (coffee == flavors[i]) {
to:
if (coffee.equals(flavors[i])) {
When comparing objects with ==, they will only be equal if they are in fact the same instance.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
String s1 = "abcdef";
String s3 = new String(s1);
if(s3 == s1){
System.out.printf("yes");
}else{
System.out.printf("no");
}
Why is not print yes? Who can explain to me?
Change s3 == s1 to s3.equals(s1).
The == operator or will check if they're both the same object, rather than what their string value is.
When you are dealing with Objects, you should use their equals method, not the ==
Check this for further explanation:
Java String.equals versus ==
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
When I debug this code I noticed that the If statement does not ever switch the boolean type sales variable to true... This is bugging me because I know that answer = "y" when it gets to the If statement. Help please! and yes I did import java.util.Scanner
Scanner input = new Scanner(System.in);
boolean sales = false;
String answer;
System.out.print("Will you be calculating the Sales department pay aswell? (y or n):");
answer = input.nextLine().trim();
if (answer == "y")
{
sales = true;
}
i have
The correct way to compare strings is:
if (answer.equals("y"))
Notice that in Java equals() is used for testing equality between objects, whereas the == operator is used for testing identity. They're two different concepts, and most of the time you're interested in equality.
As the #MadProgrammer suggests, inverting the comparison order is a good idea - it'll be safer in case answer is null:
if ("y".equals(answer))
You should use equals() to compare strings:
if (answer.equals("y")) {
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I have an if statement that takes a string, and if another string has the same value as that string do 1 thing, and if the variable doesnt equal that string do another thring
here is my code
if(Pos != "D"){
System.out.println("doesnt = D");
}
if (Pos == "D" ){//WHY ISNT THIS WORKING
System.out.println("it does = D");
}
It recognizes when the variable doesnt = D and prints "doesnt = d" but when the variable = D it does nothing. I dont know why.
thanks
Never compare Strings with == or != since these check to see if two String variables refer to the same object reference, and this is not what you're interested in. Instead use the equals(...) or equalsIgnoreCase(...) method to see if the two Strings have the same chars in the same order as that's what really matters here. i.e.,
Use equals to compare strings :
if ("D".equals(Pos))