Reputation: 163
I'm not sure why but when I use zxing in my android application to get a barcode the format returns as EAN_13 but my if staement decides it's not, then shows me EAN_13 in my toast notification. Any clues as to why it's broken?
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanResult != null) {
if (resultCode == 0){
//If the user cancels the scan
Toast.makeText(getApplicationContext(),"You cancelled the scan", 3).show();
}
else{
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT").toString();
if (format == "EAN_13"){
//If the barcode scanned is of the correct type then pass the barcode into the search method to get the product details
Toast.makeText(getApplicationContext(),"You scanned " + contents, 3).show();
}
else{
//If the barcode is not of the correct type then display a notification
Toast.makeText(getApplicationContext(),contents+" "+format, 3).show();
}
}
}
}
Upvotes: 3
Views: 279
Reputation: 1367
In java you cannot compare String with ==
so please replace these operator with .compareTo("str")
or .equals()
methods.
Upvotes: 1
Reputation: 167
For String comparison you should have to use .equals() method, actually this is doing comparison of two objects, and this method belongs Object class in java. e.g:
String val = "abc";
if(val.equals("abc")){
System.out.println("stings are equal!");
}
on other hand you can use .compareTo() function in Comparable interface, e.q:
String val = "abc";
if(val.compareTo("abc")==0){
System.out.println("stings are equal!");
}
**this is same as the above e.g.
Upvotes: 2
Reputation: 32094
In Java, you can't (well, shouldn't) use the ==
operator to compare two Strings. You should use:
if (stringOne.equals(stringTwo)) { ... }
Or, in your case:
if ("EAN_13".equals(format)) { ... }
In Java, when working with objects, the double equals operator compares two objects by reference equality. If you have two Strings:
String one = "Cat";
String two = "Cat";
boolean refEquals = (one == two); // false (usually.)
boolean objEquals = one.equals(two); // true
I say it usually won't be true because depending on how the creation of Strings is implemented in the system, it could save memory by allowing both variables to point to the same piece of memory. But, it's very poor practice to expect this to work.
Side Note: When using the strategy above, you have to ensure that the first String isn't null
, or you'll throw a NullPointerException
. If you have the ability to include external libraries in your project, I would recommend the Apache Commons Lang library, which allows:
StringUtils.equals(stringOne, stringTwo);
Upvotes: 7
Reputation: 38345
When you use ==
you're comparing the two references to see if they're the same object (ie. they point to the same address in memory), rather than comparing the values of the objects. Use format.equals("EAN_13")
instead.
Upvotes: 3