Hashey100
Hashey100

Reputation: 984

Compare integer to value inputted in TextView

Lets say I set two variables such as fnum=5, snum=6 and I set answer=fnum + snum, is there

Upvotes: 0

Views: 1137

Answers (2)

lia ant
lia ant

Reputation: 408

In your case answer1 is generic integer type You're probably confusing it with EditText or TextView as there is no getText() method for it.

You can either convert answer to string: Integer.toString(answer1) and then compare it to string from EditText

Or either by parsing EditText string value to integer (which would I do)

int userAnswer = Integer.parseInt(editText.getText().toString());
int realAnswer = snum + fnum;

if ( realAnswer == userAnswer ) {
 // hey Tim, he can do math
}

or something like that

Upvotes: 1

jzworkman
jzworkman

Reputation: 2703

Instead of casting answer1 to a String have you tried parsing the user input to a numerical value(int, or float depending on your implementation) and then using a numerical comparison? That way you arent trying to compare a string value(which could have extra white-space etc)

Upvotes: 0

Related Questions