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 7039623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:49:15+00:00 2026-05-28T01:49:15+00:00

I am making an app and the code underneath is supposed to generate 7

  • 0

I am making an app and the code underneath is supposed to generate 7 random numbers and print them to screen and none of the numbers should be same as the other.

I have tried every way of doing this. I have spent some 5 hours checking it, getting second opinions and they say it should work but it doesn’t.
What does it do? It just writes 7 random numbers to the screen but most of the time there are duplicates, most of the time the app just stops (error), but there is no error in the code. This is only one way out of many ways that I’ve tried.
Could some one please tell me if something is not right? or have a suggestion? or be so kind to give me some example code to replace to while loop? Any advice would be great bearing in mind I am quite new to java.

Update: The while loop is the main loop. it call a numbers method and that make 7 random numbers and stores them in “text_counter” array, then i check to see if they are repeated numbers in the array if not, it breaks out of the loop and prints them to the screen, however if there are it goes back to the beginning of the loop to generate more random numbers and check them again.

Here is the code

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

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

public class Lotto extends Activity {

/** Called when the activity is first created. */
Random dice = new Random();
TextView top, med, and, qcounter, wcounter, ecounter, rcounter, tcounter,
        ycounter, ucounter;
@SuppressWarnings("rawtypes")
Set uniqueItems = new HashSet();
Button gen;
EditText input1, input2;
int[] text_counter = { 1, 2, 3, 4, 5, 6, 7 };
boolean o = false;
boolean oo = false;
boolean ooo = false;
int text1;
int pre_text2;
int count = 0;
int text2;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.lotto_xml);
    gen = (Button) findViewById(R.id.b_gen);
    // ////////////////////////////////////// text view var
    qcounter = (TextView) findViewById(R.id.q_lotto);
    wcounter = (TextView) findViewById(R.id.w_lotto);
    ecounter = (TextView) findViewById(R.id.e_lotto);
    rcounter = (TextView) findViewById(R.id.r_lotto);
    tcounter = (TextView) findViewById(R.id.t_lotto);
    ycounter = (TextView) findViewById(R.id.y_lotto);
    ucounter = (TextView) findViewById(R.id.u_lotto);
    // ////////////////////////////////////////////////////
    final EditText input1 = (EditText) findViewById(R.id.et_min);
    final EditText input2 = (EditText) findViewById(R.id.et_max);

    gen.setOnClickListener(new View.OnClickListener() {

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

            o = false;
            oo = false;
            count = 0;

            while (o == false) {
                number();
                count = 0;
                for (int i = 0; i < text_counter.length; i++) {
                    if (!uniqueItems.add(text_counter[i])) {
                        count = count + 1;
                    }

                }

                if (count == 0) {
                    o = true;
                }

            }

            qcounter.setText(String.valueOf(text_counter[0]) + ",");
            wcounter.setText(String.valueOf(text_counter[1]) + ",");
            ecounter.setText(String.valueOf(text_counter[2]) + ",");
            rcounter.setText(String.valueOf(text_counter[3]) + ",");
            tcounter.setText(String.valueOf(text_counter[4]) + ",");
            ycounter.setText(String.valueOf(text_counter[5]) + ",");
            ucounter.setText(String.valueOf(text_counter[6]));

        }


        private void number() {
            // TODO Auto-generated method stub
            text1 = Integer.parseInt(input1.getText().toString());
            text2 = Integer.parseInt(input2.getText().toString());
            text2 = text2 - text1;
            text2 = text2 + 1;
            for (int i = 0; i < 7; i++) {
                text_counter[i] = text1 + dice.nextInt(text2);
            }
        }

    });

}
}
  • 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-28T01:49:16+00:00Added an answer on May 28, 2026 at 1:49 am

    You never clear the HashSet called uniqueItems. You should clear it before each loop, otherwise you will keep accumulating numbers in there forever.

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

Sidebar

Related Questions

The following code is making my app crash at line 3 without an error
I am making an iPhone app. I have a code that is in C.
I'm making a partial overlay modal in my app with the code from Semi-Modal
I am making an app that processes sensitive credit card data. If my code
I am making app that takes a screenshot of a URL requested by the
I am making app about google documents. but I don't know How to save
I'm making another app's window topmost to ensure that a click in my app
I am making an app that scrolls information on the desktop. The window is
I am making an app that will play sound on events but I can't
I'm making an App using Adobe Flex/Air. I was wondering if there is any

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.