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

  • SEARCH
  • Home
  • 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 8266673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:14:48+00:00 2026-06-08T05:14:48+00:00

This is a class file that generates random string. public class RandomGen { static

  • 0

This is a class file that generates random string.

public class RandomGen {
    static String keys() {
        String str[] = new String[25];
        str[0] = "Press A1";
        str[1] = "Press A2";
        str[2] = "Press A3";
        str[3] = "Press A4";
        str[4] = "Press A5";
        str[5] = "Press B1";
        str[6] = "Press B2";
        str[7] = "Press B3";
        str[8] = "Press B4";
        str[9] = "Press B5";
        str[10] = "Press C1";
        str[11] = "Press C2";
        str[12] = "Press C3";
        str[13] = "Press C4";
        str[14] = "Press C5";
        str[15] = "Press D1";
        str[16] = "Press D2";
        str[17] = "Press D3";
        str[18] = "Press D4";
        str[19] = "Press D5";
        str[20] = "Press E1";
        str[21] = "Press E2";
        str[22] = "Press E3";
        str[23] = "Press E4";
        str[24] = "Press E5";

        Random randomGenerator = new Random();
        int randomInt = randomGenerator.nextInt(25);
        return str[randomInt];
    }
}

This is android activity file. I’m returning a random text from a class and displaying in a text view. If the returned string is “Press A1” the user should press only the Al button. If he press other buttons an error message has to be shown in an other textview. And next time if “Press C1” displayed in that textview, the user should press only the C1 button. If he press someother button “wrong button” message has to be shown. I don’t know how to check the condition. Please help me.

    RandomGen rg = new RandomGen();
    String s;
    b[0].setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            s = rg.keys();
            tv.setText(s);
            tv2.setText("A1 is pressed");
        }
    });
    b[1].setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            s = rg.keys();
            tv.setText(s);
            tv2.setText("A2 is pressed");
        }
    });
    b[2].setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            s = rg.keys();
            tv.setText(s);
            tv2.setText("A3 is pressed");

        }
    });
    b[3].setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            s = rg.keys();
            tv.setText(s);
            tv2.setText("A4 is pressed");
        }
    });
  • 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-06-08T05:14:49+00:00Added an answer on June 8, 2026 at 5:14 am

    You have a whole lot of code duplication here. Here’s what you should do, IMHO.

    Create a static final array of texts, containing the text of all your buttons.

    Create a Map<View, String> containing the text associated with each button (unless Android allows associateing some text to a button, in which case you don’t even need this map).

    For each button add the same listener:

    View.OnCliskListener theListener = new View.OnClickListener() {
        public void onClick(View buttonClicked) {
            String textOfTheButtonClicked = map.get(buttonClicked);
            if (!textOfTheButtonClicked.equals(theTextChosenRandomly)) {
                // do what you want here
            }
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this class called PollFrame that extends JFrame in a file called PollFrame.java
This function is in a Class file in App_Code folder Public Shared Function CRUD(ByVal
import java.util.*; import java.io.*; public class LootGenerator{ public static void main (String[] args) throws
I am receiving an error with this code that pops up Class file editor:
I have this class file call SMSHelper.cs First I just wanted to know is
i make one class file for jar. and this class have use wurfl. and
I use this class to parse a .css file. https://github.com/sabberworm/PHP-CSS-Parser I thought it's easy
I have this class: class View(object): def main_page(self, extra_placeholders = None): file = '/media/Shared/sites/www/subdomains/pypular/static/layout.tmpl'
There is a python file named BasePlat.py which contain something like this: class BasePlatform(SimObj):
We have a class Car defined like this in a car.rb file class Car

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.