1

In the onFinish method of a timer I have, I want the user to have a point added to their score if their input is equal to a random number (LoadG1). The IDE wants me to use an array and score to final. I followed its advice to remove errors but failed to achieve my goal. I spent time trying to code it in different ways (e.g: using parseInt and ==) but received the same output. I don't get what I'd expect, and instead receive "Score: [l@fea28db".

public void onFinish() {
                    number.setVisibility(View.GONE);
                    final TextView prompt = (TextView) findViewById(R.id.prompt);
                    prompt.setText(" Enter the number");
                    final EditText input = (EditText) findViewById(R.id.enterAnswer);
                    input.setVisibility(View.VISIBLE);
                    input.setOnKeyListener(new View.OnKeyListener() {
                        @Override
                        public boolean onKey(View v, int keyCode, KeyEvent event) {
                        if (event.getAction() == KeyEvent.ACTION_DOWN){
                            switch (keyCode){
                                case KeyEvent.KEYCODE_ENTER:
                                    Editable answer = input.getText();
                                    input.setVisibility(View.GONE);
                                    prompt.setVisibility(View.GONE);
                                    if (answer.equals(loadG1)){
                                        score[0]++;
                                    }
                                    return true;
                                default:
                                    break;
                            }
                        }
                            return false;
                        }
                    });
                }

Here is where I output the score:

 public void onFinish() {
                    TextView result = (TextView) findViewById(R.id.outcome);
                    result.setText("Score: "+ score);
                    TextView prompt = (TextView) findViewById(R.id.prompt);
                    prompt.setVisibility(View.GONE);
                }

I've also tried doing score=score+1 and score+=1, with not difference in output. I'd greatly appreciate any help.

User44
  • 140
  • 8

1 Answers1

0

If score is an array, you probably need to dereference it, looks like right now you are printing the toString() of an array, which isn't the value you are actually looking for.

Try doing result.setText("Score: " + score[0]); instead.

The IDE probably suggested using a final array because you are passing a local variable into an anonymous inner class, but wanted to change it's value and track it later. Found out more about this pattern here: Why are only final variables accessible in anonymous class? (Ivan Dubrov's answer is particularly relevant).

Community
  • 1
  • 1
emerssso
  • 2,376
  • 18
  • 24
  • This helped, I had to use parseInt (String.valueof) and == for the score to actually go up. Essentially, they had to both be converted to integers for a point to be added. Your suggestion of bringing the array back at the end got rid of the weird output error and gave me a 0. Thanks for the response. – User44 Oct 12 '16 at 17:33