This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
Hello I am having a some difficulty with a very simple program.
import java.util.*;
public class Compare
{
public static void main( String args[] )
{
String username;
Scanner input = new Scanner(System.in);
String users[] = {"John", "James", "Smith", "Paul"};
System.out.println("Please Enter Username: ");
username = input.next();
for( int i = 0; i < users.length(); i ++ )
{
if( users[i] == username )
System.out.println("Match");
else
System.out.println("No Match");
}
}
}
When I run this program I get No Match which I don't know how is possible when I enter the same string as on of the strings in the users array. Any Suggestions?
use .equals method to compare strings ( and any other objects ) not == operator.
for instance:
users[i].equals(username);
== operator used with reference variables checks if they are pointing at the same spot in the memory.
You need to use users[i].equals(username).
== uses reference, not value comparison.
You need to write
if (users[i].equals(username))
Don't use == to compare Strings in Java, because it determines whether two Strings are the exact same object in memory; whereas equals actually compares the characters in the Strings.
Related
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed last year.
I want to know how to compare string to a variable.
For example I want to check if the input taken is run and if it's not print that is not a command
class Main
{
public static void main(String args[])
{
System.out.println("a pokrmon appeard");
System.out.println("what would you like to do");
Scanner scan = new Scanner(System.in);
Random genrator = new Random();
String input =scan.nextLine();
int genrate;
genrate=genrator.nextInt(4);
String fail ="failed to escape";
String escaped ="got away safly";
if (genrate <= 2){
System.out.println(escaped);
}
else{
System.out.println(fail);
}
};
I have tried using some methods like if(input=="run") but it doesn't work
Salam (Hello in Muslim) guy.
To compare a string with another string, you need to call the string1.equals(string2) method.
It compares the value of string1 with string2 and if the values match, it returns true otherwise false.
string1==string2 - compares references to objects of type string, not the values of these objects. You can read more about this here How do I compare strings in Java? . Good luck learning Java. (I'm from Russia, I'm writing from a translator)
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 6 years ago.
I've been trying to create a program that censors a word but I was having difficulty with that so I tried going back to some of the fundamental code and testing it and I am coming across an odd result.
import java.util.Scanner;
public class TextCensor
{
public static void main(String[] args)
{
String input;
Scanner keyboard = new Scanner(System.in);
input = keyboard.nextLine();
int length = input.length() - 1;
if (length + 1 >= 3)
{
for (int i=0; i<(length - 1); i=i+1 )
{
char first = input.charAt(i);
char second = input.charAt(i+1);
char third = input.charAt(i+2);
String censorCheck = "" + first + second + third;
if (censorCheck == "tag")
{
System.out.println("success");
}
else
{
System.out.println(censorCheck);
}
}
}
else
{
System.out.println(input);
}
}
}
If I input the string "adtag" I will obtain the following output:
adt
dta
tag
yet "success" will never be printed despite the fact that I have printed a censorCheck that is equal to "tag".
String is an object. You have to compare objects by equals():
censorCheck.equalsIgnoreCase("tag")
Ignore case works fir upper letters as well.
Only for primitives you can use comparison by ==:
3 == 3
You are trying to check whether both instance of String is same or not instead of checking contents of both string.
You should try censorCheck.equals("tag") .
To compare whether contents of two string are equal or not in JAVA you should use the equals() method. You cannot compare the value of two string by the == operator . In your case use if (censorCheck.equals("tag")) and see if you get the desired result.
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)
Closed 9 years ago.
I'm currently working on a financial planning app for class but I cant get a loop with a condition inside it to work. It just keeps looping despite the condition - it's almost as if the condition is being ignored completely.
Here's my code - please help!
while (true){
Scanner scanVar = new Scanner(System.in);
System.out.println("\nEnter expenditure item: ");
String myString = scanVar.nextLine();
Scanner scanVar2 = new Scanner(System.in);
System.out.println("\nEnter expenditure value: ");
double myDouble = scanVar2.nextDouble();
expenditureMap.put(myString, myDouble);
Scanner scanVar3 = new Scanner(System.in);
System.out.println("\nAnother item? ");
String myString2 = scanVar3.nextLine();
if (myString2 == "yes") {
continue;
}
else {
break;
}
}
Many thanks,
Dylan
You really want to be using mystring2.equals("yes") (or even better, "yes".equals(mystring2) )
The == operator on objects tests for them being the identical instance, not the same string values....
String a = new String("yes");
String b = new String("yes");
a == b => false
a.equals(b) => true
If you are using the == operater it is comparing if the object references match. You should use the equals operator
if (myString2.equals("yes"))
change the condition as follows and then try:
if (myString2.equals("yes")) {
You shall use equals ... check this post
How do I compare strings in Java?
reference comparison means checking if both objects have the same address in memoery
value comparison means checking the value inside the objects
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]))