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);
}
}
});
}
}
You never clear the HashSet called
uniqueItems. You should clear it before each loop, otherwise you will keep accumulating numbers in there forever.