Caleb Novess
Caleb Novess

Reputation: 43

Android while loop causing app to crash

I am designing an app for my work that takes in SKU's from a bluetooth scanner and holds them in a edittext. I am trying to program a delete button that when it is pressed it will use a substring to take delete the last sku entered. I was trying to use a while loop and line counts to keep taking a substring of one less character until my line count had gone down one number. Everytime I try to do this the application crashes. Example Code

Upvotes: 0

Views: 952

Answers (3)

Little Santi
Little Santi

Reputation: 8803

By the code you have posted, none of the variables lineCount nor realLineCount do change within the loop, so once the execution enters into the loop it can never exit, because the exit condition is never reached.

You must ensure that at least one of those variables increases or decreases so that the exit condition may be reached at some time.

Upvotes: 1

Quarcki 05
Quarcki 05

Reputation: 26

You should declare your variables before the loop starts, and not in the loop.

Upvotes: 1

Thecave3
Thecave3

Reputation: 797

You must not declare variables inside loops. Declare all outside and it will work (or at least it will not crash)

Upvotes: 0

Related Questions