-1

Can anyone see any errors in this code? I've checked to make sure the data has been written to the database. Every time I click the button it gives me the "Incorrect Password" text. It's probably something stupid that I'm overlooking. Any help is appreciated.

public void buttonWork() {
    button_credCheck.setOnClickListener(new View.OnClickListener() {
        String rpq = regPwdQuery().toString();
        String passInputStr = editText_pwdInput.getText().toString();

        @Override
        public void onClick(View v) {
            if (passInputStr == rpq) {
                Intent myIntent = new Intent(LogInActivity.this, FindInfoActivity.class);
                startActivity(myIntent);

            } else {
                Toast.makeText(LogInActivity.this, "Incorrect Password", Toast.LENGTH_LONG).show();
            }
        }
    });
}

EDIT: This is not a duplicate of the referenced question because changing == to equals() did not fix my problem.

EDIT 2: Thought maybe I should Include the cursor class for regPwdQuery

 public Cursor regPwdQuery() {
        String regPwdData = editText_pwdInput.getText().toString();
        String regQuery = "SELECT * FROM UsrPass_table WHERE Pwrd ='" +   regPwdData + "'";
        SQLiteDatabase uDB = usrDB.getReadableDatabase();
        Cursor result = uDB.rawQuery(regQuery, null);
        return result;
thatdude1087
  • 155
  • 1
  • 10
  • That essentially means ***passInputStr != rpq*** , You could try the ***.equalsIgnoreCase()*** method or the ***.equals()*** method. That is because you are comparing strings! You can read more about this [here](http://www.programmerinterview.com/index.php/java-questions/java-whats-the-difference-between-equals-and/) – Skynet Aug 19 '15 at 13:11
  • @ranjith I will once one does.. right now my issue hasn't been resolved. – thatdude1087 Aug 19 '15 at 13:56

7 Answers7

2

The == operator is used when we have to compare the String object references. If two String variables point to the same object in memory, the comparison returns true.

The equals method is used when we need to compare the content of the text present in the String objects. This method returns true when two String objects hold the same content (i.e. the same values).

To compare two strings for equality, use equals( ).

public void buttonWork() {
    button_credCheck.setOnClickListener(new View.OnClickListener() {
        String rpq = regPwdQuery().toString();
        String passInputStr = editText_pwdInput.getText().toString();

        @Override
        public void onClick(View v) {
           if (passInputStr.equals(rpq))
                Intent myIntent = new Intent(LogInActivity.this, FindInfoActivity.class);
                startActivity(myIntent);

            } else {
                Toast.makeText(LogInActivity.this, "Incorrect Password", Toast.LENGTH_LONG).show();
            }
        }
    });
}
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
1

You can not compare Strings in java with == operator. Use equals() method instead:

if (passInputStr.equals(rpq))
Lamorak
  • 10,957
  • 9
  • 43
  • 57
Stefano
  • 3,127
  • 2
  • 27
  • 33
1

That essentially means passInputStr != rpq , You could try the .equalsIgnoreCase() method or the .equals() method. That is because you are comparing strings! You can read more about this here

Skynet
  • 7,820
  • 5
  • 44
  • 80
1

your comparisons checks the refferences instead the actual content of the strings. Instead use .equals() to check the value of the String.

if (passInputStr == rpq)

Should be changed to:

if(passInputStr.equals(rpq))

EDIT:

you toString the result you get from the query:

String rpq = regPwdQuery().toString();

if your result only contains the password it might work, but otherwise I don't think it will give the correct result. Since I don't know how the table UsrPass_table looks like I can only guess but I think something like this has to be implemented:

public String regPwdQuery() { String regPwdData = editText_pwdInput.getText().toString(); String regQuery = "SELECT * FROM UsrPass_table WHERE Pwrd ='" + regPwdData + "'"; SQLiteDatabase uDB = usrDB.getReadableDatabase(); Cursor result = uDB.rawQuery(regQuery, null); String password = ""; while(result.moveToNext()){ password = result.getString(0); //Parameter should match the column from where you get your data, normally 0 is an ID. } return password;

Hope it helps.

Kezufru
  • 123
  • 10
  • You Rock! Been working on this wayyyyy too long. – thatdude1087 Aug 21 '15 at 17:57
  • So apparently I was a little overtired when I thought this worked properly.. I was ready to release my app and doing everything I could think of to do wrong(which apparently I hadn't done yet) and upon entering an incorrect password I found It is actually accepting anything as a correct password.. Any ideas of what could be going wrong? If you need me to post any code just ask. Thanks – thatdude1087 Aug 25 '15 at 00:56
  • Could you add your new code? can't see anything wrong here. – Kezufru Aug 26 '15 at 17:18
  • I've actually opened a new question because I changed my query to getCount instead of comparing strings..... check here http://stackoverflow.com/questions/32212964/error-in-either-if-statement-or-in-query – thatdude1087 Aug 26 '15 at 18:02
0

Try the

 if (passInputStr.equals(rpq))

reference:

What is the difference between == vs equals() in Java?

Here is an example (you can run it):

public final class MyEqualityTest
{
    public static void main( String args[] )
    {
        String s1 = new String( "Test" );
        String s2 = new String( "Test" );

        System.out.println( "\n1 - PRIMITIVES ");
        System.out.println( s1 == s2 ); // false
        System.out.println( s1.equals( s2 )); // true

        A a1 = new A();
        A a2 = new A();

        System.out.println( "\n2 - OBJECT TYPES / STATIC VARIABLE" );
        System.out.println( a1 == a2 ); // false
        System.out.println( a1.s == a2.s ); // true
        System.out.println( a1.s.equals( a2.s ) ); // true

        B b1 = new B();
        B b2 = new B();

        System.out.println( "\n3 - OBJECT TYPES / NON-STATIC VARIABLE" );
        System.out.println( b1 == b2 ); // false
        System.out.println( b1.getS() == b2.getS() ); // false
        System.out.println( b1.getS().equals( b2.getS() ) ); // true
    }
}

final class A
{
    // static
    public static String s;
    A()
    {
        this.s = new String( "aTest" );
    }
}

final class B
{
    private String s;
    B()
    {
        this.s = new String( "aTest" );
    }

    public String getS()
    {
        return s;
    }

}
Community
  • 1
  • 1
Ranjithkumar
  • 16,071
  • 12
  • 120
  • 159
0
    public void buttonWork() {
    button_credCheck.setOnClickListener(new View.OnClickListener() {


        @Override
        public void onClick(View v) {
        String rpq = regPwdQuery().toString();
        String passInputStr = editText_pwdInput.getText().toString();
            if (passInputStr == rpq) {
                Intent myIntent = new Intent(LogInActivity.this, FindInfoActivity.class);
                startActivity(myIntent);

            } else {
                Toast.makeText(LogInActivity.this, "Incorrect Password", Toast.LENGTH_LONG).show();
            }
        }
    });
}

Try this.. Put rpq and passInputStr in onClick method. also change if condition if (passInputStr.equals(rpq))

Pankaj Kharche
  • 1,329
  • 4
  • 15
  • 37
  • I tried your suggestions and no change.. I've added my cursor class just to make sure although I don't think there is an error there. – thatdude1087 Aug 19 '15 at 13:57
0

Simply, replace:

if (passInputStr == rpq)

with

if (passInputStr.equals(rpq))

Use string.equals(anotherString) method when you want to compare between two different strings.

Edit:

public String regPwdQuery() {
    String regPwdData = editText_pwdInput.getText().toString();
    String regQuery = "SELECT * FROM UsrPass_table WHERE Pwrd ='" + regPwdData + "'";
    SQLiteDatabase uDB = usrDB.getReadableDatabase();
    Cursor cursor = uDB.rawQuery(regQuery, null);
    //Modify this
    String result = cursor.getString(cursor.getColumnIndex("password"));
    return result;
Hussein El Feky
  • 6,627
  • 5
  • 44
  • 57
  • please read the other answers as well as my complete question before wasting both our times saying the same thing other people have already said... As noted in the Edit to my question, this did not solve the problem. – thatdude1087 Aug 19 '15 at 14:46
  • @thatdude1087 I have just read your edited question now. To know why both strings are never equal, you should make sure what is the result of 'regPwdQuery().toString();' Try for example Log.d("Test", regPwdQuery().toString()); then see what's the result and reply to me please so that I can help you... The problem must be in there. – Hussein El Feky Aug 19 '15 at 15:25
  • @thatdude1087 I have also updated my answer, make sure you write your correct column name, then test it. – Hussein El Feky Aug 19 '15 at 16:05
  • only thing is a Cursor can't return a String... I changed the class to a String instesad of a Cursor and got this in my logcat: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0 – thatdude1087 Aug 20 '15 at 18:25
  • Did you write your correct column name? I wrote it as password from my random guess. I don't know what did you call it. – Hussein El Feky Aug 20 '15 at 19:26
  • yeah I put it in as it's written in the DB. I don't understand, my app has the user register a password on the first screen(which that password is written to a DB) and then on the following screen it asks you to log in with the password(which is where the query comes in).. now when i register a password the app stops working and i get the: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0 error.. before it was at least getting to the second screen – thatdude1087 Aug 21 '15 at 17:46