jeudi 16 septembre 2021

Java else-if statement gives wrong output [closed]

I have a database application in which the user shall select a table through and if-else statement. With only two statements it works fine. However, with more tables I have problems to select that proper table. My code is:

private Context context;
private DatabaseHelper databaseHelper;
private SQLiteDatabase database;
private String field = TABLEA;

public MyAppHelper(Context context) {
    this.context = context;
}

private void choice(boolean choice){
    if (choice){
        field = TABLEA;
    }
    else if(choice){
        field = TABLEB;
    }
    else if (choice){
        field = TABLEC;
    }
    else {
        field = TABLED;
    }
    

The result is, if the user selects TABLEA, the application selects TABLEA (what I want).

The problems start with the next statement:

If the user selects TABLEB, the application selects TABLEB and TABLEC.

If the user selects TABLEC, the application selects TABLEA.

If the user selects TABLED, the application selects TABLEB, TABLEC and TABLED.

So, obviously there is something wrong with the syntax, but I don't get it. I don't want to try a totally different approach at this moment (like for instance switch) because in principle the approach works. So, any idea how to do this correctly with the else-if approach?

Aucun commentaire:

Enregistrer un commentaire