why String from intent cant use in "if clause"? [duplicate] - java

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I compare strings in Java?
i have 2 class, let say a.class and b.class
and i want to send array from a to b using intent, in a.class
Intent intent = new Intent(this,b.class);
intent.putExtra("stringtext", "1");
startActivity(intent);
and in b.class i catch the intent value with this
Intent it = getIntent();
String id = it.getStringExtra("stringtext");
when i try to print id, it give me "1"
but when i'm using id in if clause i didnt work, i try this
if(id=="1")
{
teks.setText("its one");
}
else
{
teks.setText("not one";
}
how could this happend?

Use equals() method to compare string, == compares the reference for Object
Make it
if("1".equals(id))
{
teks.setText("its one");
}
See
Java String.equals versus ==
Interview : Java Equals

Related

can not match the String get from properties file in Java [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
I have a line in file config.properties
clean=true
and use following code to get this property
private static String clean;
Properties prop = new Properties();
try {
prop.load(new FileInputStream("config.properties"));
clean = prop.getProperty("clean");
}
I use System.out.println(">"+clean+"<") to see the output and get ">true<", which indicates there is no blank, no \n
However, when I use
if (clean == "true") {
// program does not go here
}
else {
// program goes here
}
what is the possible reason?...
Try the following:
== checks if both objects point to the same memory location whereas .equals() evaluates to the comparison of values in the objects.
if (clean.equals("true")) {
// program does not go here
}
else {
// program goes here
}
The Problem is you are using equality operator which doesn't compare literal instead references. So you have to use equals method to do literal check
if ("true".equals(check)) {
// Now Program will go here
}
else {
// and now here
}

Why following code printing False? [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
Replace will create new object and both side this new will be compared. then why it showing false.
When exactly created new string will be added in string pool?
if("String".replace("g", "G") == "String".replace("g", "G"))
{
System.out.println("True");
} else {
System.out.println("False");
}
because replace() will always return a new String instance. So the 2 same calls to replace method will return 2 different instances with same value.
use equals() instead of == if you want to compare value
Use intern() on both replaced values if you want to add the string to the string constants pool (and are bent on using == :P)
if ("String".replace("g", "G").intern() == "String".replace("g", "G").intern()) {
System.out.println("True");
} else {
System.out.println("False");
}
}
OP :
true

Android: Why is my if statement not executing inside the onClick method [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
When I print out the user value and the pass value into LogCat, the values are printed out as "A" and "a". If the user value is "A" and the pass value is "a", I don't understand why the if statement doesn't execute. When I place the contents that are inside the if statement outside of the if statement, the startActivity() method operates correctly. This tells me that the if statement is the problem but I don't see how. I don't get it!
public void onClick(View v)
{
String user = username.getText().toString();
String pass = password.getText().toString();
System.out.println(user);
System.out.println(pass);
if (user == "A" && pass == "a")
{
Intent intent = new Intent("com.example.android.STARTINGPOINT");
startActivity(intent);
}
}
Use the String#equals method to compare strings instead of ==:
if (user.equals("A") && pass.equals("a"))

Java if Statement under enhanced for loop did not work [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
This is a code fragment that I am working with for a logon system using hashset Users.
I checked the enhanced for loop part it works but the if statement seems like it never works out, even if the values are equal.
if (comm.equals("Sign in")) {
user urs = new user(p_name, p_pass, p_id );
Iterator<user> iter = Users.iterator();
for(user obj : Users) {
if (p_u == obj.getUsername()&& p_pw == obj.getPassword ()&& p_sn== obj.getStudentID()) {
JOptionPane.showConfirmDialog(
jf,
"Success", "Success",
JOptionPane.DEFAULT_OPTION);
break;
} else {
log_in.setText("Try Again");
exit.setText("Create User");
}
Remember, in Java == operators test the equality of the value, in this case, two references. These two references do not point to the same memory location and therefore will never equate to true. Use the equals() method to test for equality of the values of strings.
I think you need to changed it to something like this:
if (comm.equals("Sign in")) {
user urs = new user(p_name, p_pass, p_id );
Iterator<user> iter = Users.iterator();
for(user obj : Users) {
if (p_u.equalsIgnoreCase(obj.getUsername())&& p_pw.equals(obj.getPassword())&& p_sn.equals(obj.getStudentID())) {
JOptionPane.showConfirmDialog(
jf,
"Success", "Success",
JOptionPane.DEFAULT_OPTION);
break;
} else {
log_in.setText("Try Again");
exit.setText("Create User");
}
Assuming that p_u, p_pw and p_sn are String object references the code below is checking whether those two objects are the same object (or whether their refences to the object's position in memory are the same) rather than whether the String objects share the same character sequence.
if (p_u == obj.getUsername()&& p_pw == obj.getPassword ()&& p_sn== obj.getStudentID())
Instead, to check whether their character sequence matches up you should use the .equals() method. Example below;
if (p_u.equals(obj.getUsername())&& p_pw.equals(obj.getPassword ())&& p_sn.equals(obj.getStudentID()))
I hope this helps.

String comparison to constant error [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I compare strings in Java?
I have this code its working fine in retreiving the value from the url, but its not recognizing that the string is "True" is the toString() what I need or something else?
try {
URL url = new URL("http://www.koolflashgames.com/test.php?id=1");
URLConnection yc = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(yc
.getInputStream()));
inputLine = in.readLine();
inputLine = inputLine.toString();
if(inputLine == "True") {
logger.info(inputLine);
player.sendMessage("Thanks");
}else{
logger.info(inputLine);
player.sendMessage("HAHAHA");
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
You cannot use == to compare the content of Strings, as they are objects. You have to create a method to compare objects. In the case of strings, you can use stringName.equals(otherString).
I beg to differ. Use .equalsIgnoreCase() method to compare the string ignoring the case. This will match all cases, such as "True", "TRue", "tRue".. etc approximately 16 matches.
You must use equals to compare strings. Replace:
if(inputLine == "True") {
with:
if(inputLine.equals("True")) {
The operator == tells you if two references refer to the same object, not if the values are the same.
In order to compare String objects, use the equals() method.
The == operator checks whether the two Strings have the same reference.
See How do I compare strings in Java? for more info.

Categories