0

Read below. I got it fixed.

Yes I've asked this already and yes people suggested me to use .equals but it still doesn't work.

My problem is that my code doesn't breaks the for loop.

for(int l=0; l<1000; l++){
    if(settings.getString("note" + l, "").equals("")){
        if(settings.getString("notelong" + l, "").equals("")){
            editor.putString("note" + l, title.getText().toString());
            editor.putString("notelong" + l, note.getText().toString());
            editor.commit();
            break; // This should break the loop BUT IT DOESN'T...
        }
    }
}

Any suggestions?

EDIT: Yes it runs break. Else it wouldn't create the notes

EDIT 2: Sorry, this for loop was in another for loop and I forgot to break the first one... now it works! Thanks for your help :)

4
  • 5
    Does settings.getString("note" + l, "").equals("") even ever return true? Use Log.i() to determine that.
    – Ahmad
    Aug 5, 2013 at 21:53
  • @bellabax. If break doesn't work. That wouldn't work either.
    – Rohit Jain
    Aug 5, 2013 at 21:55
  • 5
    Check your assumptions; I'm reasonably certain break works as advertised. Aug 5, 2013 at 21:55
  • Add SOP after the break; statement. if it doesn't work - it should print to catlog.
    – Nir Alfasi
    Aug 5, 2013 at 21:57

3 Answers 3

2

Either

settings.getString("note" + l, "")

or

settings.getString("notelong" + l, "")

(or both) do not return an empty string. If they don't, your equals comparisons return false. In which case, it never gets into break statement. For your break logic to work, both if statements need to be true.

1
  • @user2124499 So it goes inside if it is 1000, for the rest it doesn't result to be true. You can remove the loop and have the value of l to be 0 or 1 and see if it goes in.
    – Saj
    Aug 8, 2013 at 9:34
1

remove your first if, and check if your second if is equal true on any iteration.

1

Make sure that the break; line is actually being run. Do both the if statements equal true on the same iteration? That could be the problem.

Use the debugger in eclipse and walk through it.

Your Answer

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