0

Here is my edited code:

package com.gamesup.braingame;

import java.util.Random;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Easy extends Activity implements OnClickListener{

EditText display;
// This Array says , I am an array that holds arrays
String [][] multiArray = {{"4 + 5", "9"},     
        {"20 * 3","60"},
        {"99 - 9","90"}};  
TextView displayExpression;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.easy);

    display = (EditText)findViewById(R.id.displayText);
    display.setText("?");

    displayExpression = (TextView) findViewById(R.id.expression);
    Button generate = (Button) findViewById(R.id.random_gen);

    generate.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Random ranGenerate = new Random ();
            int random = ranGenerate.nextInt(4) ;
            // Fetch your random question
            String Rquestion = multiArray[random][0];
            displayExpression.setText(Rquestion);

        }
    });

}

static boolean isEmpty = true;

public void num_Clicked(View v){
    Button btn = (Button) findViewById(v.getId());
    //getting the button object and using a view to get the id of the buttons

     if (v.getId()== R.id.del_button){
            String s = display.getText().toString();
            s = s.substring(0, s.length() - 1);
            display.setText(s);
            return;
     }


    if(isEmpty){
        display.setText(btn.getText());
        isEmpty = false;
    }
    else{
        display.append(btn.getText().toString()); 
    }



}

public void hash_Clicked(View v){

    if (v.getId()== R.id.hash_button){


         // Get the Answer from your EditText
         String answer =  display.getText().toString();

         // Using a for loop iterate on the base index
         for(int i = 0; i < multiArray.length ; i++)
         {
              // if the answer is in position 1 of Array [i] 
              if(answer.equals(multiArray[i][1]))
              {
                // We have found the answer, Congratulate the User 
                  displayExpression.setText("CORRECT");

              }else{
                  displayExpression.setText("INCORRECT");

              }

         }

    }

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

}


}

After the user enters the wrong answer and clicks the random generator button (random_gen) again my app closes with an error. Does this have anything to do with the multiArray being created in the onClick method? What would i have to do to resolve this?

EDIT:

Now when i press the hash button it does not display correct, Do i have to add a different if statement for each position?

1
  • I suggest you accept one of the answers, and formulate a new question with the matter of the display of your hash button. Commented Feb 20, 2014 at 22:33

2 Answers 2

1

Your ranGenerate will generate integers from 0 to 3, and your array indexes go from 0 to 2, so if random happens to be 3, you get an exception.

Change the maximal generated integer to 2 with the following:

int random = ranGenerate.nextInt(3);  //this generates integers from 0 to 2

or even better - use the length of your array, so that if you add more to it later, you won't have to change the random generation code:

int random = ranGenerate.nextInt(multiArray.length);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks :) didn't see that. But now when i press the hash button it does not display CORRECT even if the answer is correct, Do i have to add a different if statement for each position? It displays INCORRECT each time.
0
 Random ranGenerate = new Random ();
 int random = ranGenerate.nextInt(4) ;

nextInt(4) can give values range 0 - 3. and your array have values from 0 - 2.

Change to int random = ranGenerate.nextInt(3) or add another item to array.

1 Comment

Thanks :) didn't see that. But now when i press the hash button it does not display CORRECT even if the answer is correct, Do i have to add a different if statement for each position?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.