Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android do while loop

Hi This my second question here. I have the following table

|-----|-------|------|------|
|._id.|..INFO.|.DONE.|.LAST.|
|..1..|...A...|...N..|......|
|..2..|...B...|...Y..|..L...|<--- cursor.moveToPosition((int)_id-1);
|..3..|...C...|...Y..|......|
|..4..|...D...|...Y..|......|
|..5..|...E...|...N..|......|
|..6..|...F...|...N..|......|
|-----|-------|------|------|

I use the code:

cursor = db.query(TABLE_NAME, new String[]{INFO,DONE,LAST},null,null,null,null,null);
cursor.moveToPosition((int)_id-1);
String Yval = cursor.getString(cursor.getColumnIndex(DONE));
do
{
    cursor.moveToNext();
    Yval= cursor.getString(cursor.getColumnIndex(DONE));
}
while (Yval=="Y");
s = Yval;

I initially point the cursor to the LAST row I accessed, then I make a loop to go through the values in the DONE column, not stopping if there are Y's in the row of the column. When an N appears in the loop, the loop should stop. But it doesn't work. Yval never equals "Y". So the cursor does one 'moveToNext' and then exits the loop, because it doesn't read Yval as a "Y". (I also changed everything to integers. 1 for N, and 0 for Y, but it still didn't work) So what am I doing wrong here?

like image 695
Peter James Avatar asked Jul 05 '12 16:07

Peter James


People also ask

What are the differences between while and do while loop?

The do-while loop is very similar to that of the while loop. But the only difference is that this loop checks for the conditions available after we check a statement. Thus, it is an example of a type of Exit Control Loop.

How do you do a boolean in a while loop?

Syntax. do { // Statements }while(Boolean_expression); Notice that the Boolean expression appears at the end of the loop, so the statements in the loop execute once before the Boolean is tested. If the Boolean expression is true, the control jumps back up to do statement, and the statements in the loop execute again.

Do while Java vs while?

Java do-while loop is used to execute a block of statements continuously until the given condition is true. The do-while loop in Java is similar to while loop except that the condition is checked after the statements are executed, so do while loop guarantees the loop execution at least once.


2 Answers

So you have to use equals() method if you want to compare Strings

while (Yval.equals("Y"));

You should know that:

  • == tests for reference equality.
  • equals tests for value equality.

So you want to test if Yval String has Y value so you have to use equals() method.

You approach doesn't work bacause:

String data = "lorem";
data == "lorem" ==> FALSE
data.equals("lorem") == TRUE


Also make sure that your Cursor has valid row so you need to add to condition also cursor.moveToNext() so

cursor.moveToNext() && (Yval.equals("Y")

also you need to treat cursor.moveToPosition((int)_id-1) so add it to condition.

like image 66
Simon Dorociak Avatar answered Sep 22 '22 01:09

Simon Dorociak


I recommend changing a few things:

if(cursor.moveToPosition((int) _id - 1)) {
    int doneIndex = cursor.getColumnIndex(DONE);
    String Yval;
    do {
        Yval = cursor.getString(doneIndex);
    } while(Yval.equals("Y") && cursor.moveToNext());
}
  • You should check if a row exists at position _id - 1 since the SQLite _id is a unique id, not the position of a row in a Cursor.
  • You only need to fetch the index of the DONE column once, simply store it in a local variable.
  • As deceiver mentioned, String are tested with equals() and similar methods.
  • You need consider what happens if all of the rows are "DONE", then you must stop trying to read the Cursor before an out of bounds exception is thrown.
like image 42
Sam Avatar answered Sep 19 '22 01:09

Sam