简体   繁体   中英

Getting values from an edittext and comparing it to a set variable

Can anyone spot what I'm doing wrong here. When I call checkAnswer(), it crashes the program. Here are the variables and my button.

int random1;
int random2;

 int Min = 1;
 int Max = 4;
 int score = 0;
 int correctAnswer;
 String question;

String i;
String a;

  editText = (EditText)findViewById(R.id.Edit);
   editTextEquation = (TextView)findViewById(R.id.equation);
  scoredisplay = (TextView)findViewById(R.id.score);


@Override
public void onClick(View v){
switch(v.getId()){
case R.id.keypad_hash:

    getQuestion();
   checkAnswer(editText.getText().toString());

    break;

I'm attempting to extract the value of the user input edittext and then comparing it with the answer.

 public void getQuestion(){

    random1 = (int)(Math.random()*100);
    random2 = (int)(Math.random()*10);

    int i1 = Min + (int)(Math.random() * ((Max - Min) + 1));

    if(i1 == 1){
    question = (random1 + "+" + random2);
    correctAnswer = random1 + random2;
    editTextEquation.setText(question);
    }
    if(i1 == 2){
    question = (random1 + "-" +random2);
    correctAnswer = random1 - random2;
    editTextEquation.setText(question);

    }
    if(i1 == 3){
    question = random1 + "/" +random2;
    correctAnswer = random1 / random2;
    editTextEquation.setText(question);

    }
    if(i1 == 4){
    question = (random1 + "*" +random2);
    correctAnswer = random1 * random2;
    editTextEquation.setText(question);


    }



}

public void checkAnswer(){
    i = editText.getText().toString();
    Integer a = Integer.valueOf(i);
    if (a == correctAnswer){

        score++;
        scoredisplay.setText(score);

    }
    else{

        score--;
        scoredisplay.setText(score);
    }




}

You should call setText with a String . If you call it with an int it tries to find the string that matches that id. So it should probably be:

scoredisplay.setText("" + score);

Also, compare strings using .equals , not == , as == compares references, not content.

You are passing parameters to your checkAnswer() method, and it isn't expecting any.

Either make your method call to check answer have no parameters like so:

getQuestion();
checkAnswer();

or change your checkAnswer method to accept a string parameter. Like this:

public void checkAnswer(String answer){
    i = answer;
    Integer a = Integer.valueOf(i);
    if (a == correctAnswer){

        score++;
        scoredisplay.setText(score);

    }
    else{

        score--;
        scoredisplay.setText(score);
    }




}

Aren't you getting any compile error! , or post the latest code of yours... checkAnswer(String) you are calling is different first of all from checkAnswer() defined below.. you also haven't shown editText declaration..

Also it would break at

Integer a = Integer.valueOf(i);

if i(edittext) has any string value,, you should catch exception there.. and use i.trim() for proper handling.

And provide atleast stacktrace and proper code to let us know the problem...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM