java - boolean not holding value when method is executed -
i have method having problem with. second method below, promptforpinnumber(), calls first method, canconverttointeger(), , carries out action, dependent on whether value of boolean variable, pinvalid, true or false.
when execute method canconverttointeger() on own, functions fine, , value of pinvalid correct.
when execute promptforpinnumber(), , enter string throws , exception, value of pinvalid stays true, else section of if else block isn't executed, however, value of pintry 0, exception must have been caught , dealt with. why boolean pinvalid true when should false?
what should happen if invalid entry made oudialog.request box, pinvalid should set false, should change value of pintry 0, ,
public boolean canconverttointeger() { string pinattempt; { pinattempt = oudialog.request("enter pin number"); try { this.pintry=integer.parseint(pinattempt); this.pinvalid = true; } catch (numberformatexception anexception) { this.pintry=0; this.pinvalid = false; } } return this.pinvalid; } public int promptforpinnumber() { this.canconverttointeger(); if (pinvalid = true) { return this.pintry; } else { oudialog.alert("number entered not valid pin"); return this.pintry; } }
classic one, replace
if (pinvalid = true)
with:
if (pinvalid == true)
or better:
if (pinvalid)
pinvalid = 1
assignment, not expression (condition).