1

I'm using compile 'com.google.android.gms:play-services:10.2.0' (https://developers.google.com/android/reference/com/google/android/gms/vision/barcode/Barcode.html#valueFormat) to read QR code. I want to be compare my read QR to a String. If it matches, it should display a message.

  @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
        if (data != null) {
            final Barcode barcode = data.getParcelableExtra("barcode");
            // I also tried barcode.displayValue!
            final String password = barcode.rawValue;

            if (password == "123456") {
                resultText.post(new Runnable() {
                    @Override
                    public void run() {
                        resultText.setText("Sucess");
                    }
                });

When I display my QR code, it reads 123456, but comparing it to "123456" doesn't work.

I thought displayValue and rawValue were casting my QR into a string. Does anyone have any idea? Thanks

3 Answers 3

1

You would have to use password.equals("123456") to compare your strings. the == operator checks to see if the strings are the exact same object instead of having the same value.

  @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
        if (data != null) {
            final Barcode barcode = data.getParcelableExtra("barcode");
            // I also tried barcode.displayValue!
            final String password = barcode.rawValue;

            if (password.equals("123456")) {
                resultText.post(new Runnable() {
                    @Override
                    public void run() {
                        resultText.setText("Sucess");
                    }
                });
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your comment. It was simple, but well explained. =] It solved my problem.
1

Use equals it will return true if they are equal otherwise return false for details

password.equals("12345")

Comments

1

use password.equals("123456"), which is valid for comparing strings. I hope this will resolve your problem. As "==" compares the reference of the value in strings, where as .equals() compare the actual value .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.