Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7593847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:16:20+00:00 2026-05-30T21:16:20+00:00

Basically I’m trying to replace ? by pressing any keypad button. For example, if

  • 0

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…..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T21:16:21+00:00Added an answer on May 30, 2026 at 9:16 pm

    Instead of

    display.setText("1");
    

    write

    display.setText(str.replace("??", "1"));
    

    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:

    str.equals("?");
    

    Replace the code of the keypad_1 case for this one (deleting the if):

    case R.id.keypad_1:
          String str1 = display.getText().toString(); 
          display.setText(str1.replace("??", "1")); 
    break;
    

    Hope it helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically what I want to do it this: a pdb file contains a location
Basically I have some code to check a specific directory to see if an
Basically I am trying to restart a service from a php web page. Here
Basically, I'm trying to tap into the Soap pipeline in .NET 2.0 - I
Basically I'm trying to accomplish the same thing that mailto:bgates@microsoft.com does in Internet Explorer
Basically I want to know how to set center alignment for a cell using
Basically, what I'm trying to create is a page of div tags, each has
Basically there is a file called 8puzzle.py and I want to import the file
Basically what I'm trying to do is make a very simple vertical bullet projectile
Basically I am writing a simple bit of code to increment every one second

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.