Basically I’m trying to replace “?” by pressing any keypad button. For example, if the question shows as “0+1=?”, I want “1” to replace the question mark.
My code for Java is:
package org.example.question;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class QuestionActivity extends Activity implements View.OnClickListener {
/** Called when the activity is first created. */
//variable for different questions
int fnum0, snum0,fnum1, snum1,fnum2, snum2,fnum3, snum3,
fnum4, snum4,fnum5, snum5,fnum6, snum6,fnum7, snum7,
fnum8, snum8,fnum9, snum9, answer;
//variable and type declaration for buttons and text
Button keyOne;
Button keyTwo;
Button keyThree;
Button keyFour;
Button keyFive;
Button keySix;
Button keySeven;
Button keyEight;
Button keyNine;
Button keyDel;
Button keyZero;
Button keyHash;
Button keySubtract;
TextView display;
TextView display1;
TextView answer0;
int question=0;
boolean q;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//display text on screen
display1 = (TextView)findViewById(R.id.Guess);
display = (TextView)findViewById(R.id.Title);
answer0 = (TextView)findViewById(R.id.Answer);
///Code for correct and incorrect
//assigning names to each keypad
keyOne= (Button) findViewById(R.id.keypad_1);
keyTwo= (Button) findViewById(R.id.keypad_2);
keyThree= (Button) findViewById(R.id.keypad_3);
keyFour= (Button) findViewById(R.id.keypad_4);
keyFive = (Button) findViewById(R.id.keypad_5);
keySix= (Button) findViewById(R.id.keypad_6);
keySeven = (Button) findViewById(R.id.keypad_7);
keyEight = (Button) findViewById(R.id.keypad_8);
keyNine = (Button) findViewById(R.id.keypad_9);
keyZero = (Button) findViewById(R.id.keypad_0);
keySubtract = (Button) findViewById(R.id.keypad_subtract);
keyHash = (Button) findViewById(R.id.keypad_hash);
keyDel = (Button) findViewById(R.id.delete);
//setting button to produce an event when each button is pressed
keyOne.setOnClickListener(this); keyTwo.setOnClickListener(this); keyThree.setOnClickListener(this);
keyFour.setOnClickListener(this); keyFive.setOnClickListener(this); keySix.setOnClickListener(this);
keySeven.setOnClickListener(this); keyEight.setOnClickListener(this); keyNine.setOnClickListener(this);
keySubtract.setOnClickListener(this); keyHash.setOnClickListener(this); keyDel.setOnClickListener(this);
}
public void onClick(View arg0) {
switch(arg0.getId()){
case R.id.keypad_hash:
//Generates random numbers
fnum0 = (int) ((double) ((Math.random() * 10)));
snum0 = (int) ((double) ((Math.random() * 10)));
String str = "";
//genrates random number between 0 to 9
int operation = (int) ((double) ((Math.random() * 10)));
Log.d("debug", "operation value: " + operation);
if(operation == 0)
str = fnum0+ "+" + snum0+ "="+ "??";
else if(operation == 1)
str = fnum0 + "-" + snum0+ "="+ "??";
else if(operation == 2)
str = fnum0 + "*" + snum0+ "="+ "??" ;
else
str = fnum0 + "/" + snum0+ "="+ "??";
display.setText(str);
break;
case R.id.keypad_1:
if(q="?" )
{
display.setText("1");
}
else
{
return;
}
break;
case R.id.keypad_2:
display.setText("2");
break;
case R.id.keypad_3:
display.setText("3");
break;
case R.id.keypad_4:
display.setText("4");
break;
case R.id.keypad_5:
display.setText("5");
break;
case R.id.keypad_6:
display.setText("6");
break;
case R.id.keypad_7:
display.setText("7");
break;
case R.id.keypad_8:
display.setText("8");
break;
case R.id.keypad_9:
display.setText("9");
break;
case R.id.delete:
display.setText("");
break;
case R.id.keypad_0:
display.setText("0");
break;
case R.id.keypad_subtract:
display.setText("-");
break;
}
}
public void requestFocus() {
// TODO Auto-generated method stub
}
}
However at the moment its displaying the question “0+1=” but when I press one it removes the question and display “1” on its own on the screen.
UPDATE for ERROR
case R.id.keypad_1:
public void onClick(View arg0) {
String str=null;
switch(arg0.getId()){
case R.id.keypad_hash:
//Generates random numbers
fnum0 = (int) ((double) ((Math.random() * 10)));
snum0 = (int) ((double) ((Math.random() * 10)));
//genrates random number between 0 to 9
int operation = (int) ((double) ((Math.random() * 10)));
Log.d("debug", "operation value: " + operation);
if(operation == 0)
str = fnum0+ "+" + snum0+ "="+ "??";
else if(operation == 1)
str = fnum0 + "-" + snum0+ "="+ "??";
else if(operation == 2)
str = fnum0 + "*" + snum0+ "="+ "??" ;
else
str = fnum0 + "/" + snum0+ "="+ "??";
display.setText(str);
break;
case R.id.keypad_1:
if(str.equals("??"))
{
String str1 = display.getText().toString();
display.setText(str1.replace("??", "1"));
}
else
{
return;
}
break;
The application is crashing now…..
Instead of
write
The replace method indicates that replace the “??” text on the str String with the “1” text, so searches the “??” text and replace it by the “1” text on this String.
To compare Strings should use equals method, for example instead of
str==?, write:Replace the code of the keypad_1 case for this one (deleting the if):
Hope it helps.