User44 User44 - 1 month ago 9
Java Question

Incorrect random number output issue in Java (Android Studio)

I have a random number variable named 'loadG1' that is outputted after the user presses enter on an EditText. There are a few if statements at the bottom of the coe displayed below, with the purpose of determining the length of loadG1 (in terms of digits) depending on the user's score. The user gets a point added each time their input matches loadG1. My problem is that sometimes on the outputted randoms before 3 points (>=0), I get a six digit number. This is only meant to happen when they have 3 or more points. This also happens at other stages, e.g: 7 digits meant to be outputted by 8 are instead. The pattern seems to almost work perfectly, but at times one digit more than the limit is outputted for that user's score. Never one digit less, but either the right amount or sometimes one more.

Here's the code:

final Random generateG1 = new Random();
final int loadG1 = generateG1.nextInt(99999)+10000;
final TextView number = (TextView) findViewById(R.id.number);
number.setText(""+loadG1);

final CountDownTimer loop = new CountDownTimer(3500, 1000) {
@Override
public void onTick(long millisUntilFinished) {
}
@Override
public void onFinish() {
number.setVisibility(View.GONE);
final TextView prompt = (TextView) findViewById(R.id.prompt);
prompt.setVisibility(View.VISIBLE);
prompt.setText(" Enter the number");
final EditText input = (EditText) findViewById(R.id.enterAnswer);
input.setVisibility(View.VISIBLE);
input.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
Editable answer = input.getText();
int finalAnswer = Integer.parseInt(String.valueOf(answer));
int finalLoadG1 = Integer.parseInt(String.valueOf(number.getText()));
input.setVisibility(View.GONE);
prompt.setVisibility(View.GONE);
if (finalAnswer == finalLoadG1) {
score++;
}

number.setVisibility(View.VISIBLE);

if (score>=0) {
int loadG1 = generateG1.nextInt(99999)+10000;
number.setText(""+loadG1);
}
if (score>=3){
int loadG1 = generateG1.nextInt(999999)+100000;
number.setText(""+loadG1);
}
if (score>=6){
int loadG1 = generateG1.nextInt(9999999)+1000000;
number.setText(""+loadG1);
}
if (score>=10){
int loadG1 = generateG1.nextInt(99999999)+10000000;
number.setText(""+loadG1);
}

input.getText().clear();

start();


I'd appreciate it if someone could tell me what's causing this error, and how to resolve it. Thanks in advance.

Answer

You need to change the conditions.

  1. Either reverse the order of if statements , or
  2. Add < condition also to if statements.

And make use of else also.

    if (score>=10){
        int loadG1 = generateG1.nextInt(99999999)+10000000;
        number.setText(""+loadG1);
    }
    else if (score>=6){
        int loadG1 = generateG1.nextInt(9999999)+1000000;
        number.setText(""+loadG1);
    }
    else if (score>=3){
        int loadG1 = generateG1.nextInt(999999)+100000;
        number.setText(""+loadG1);
    }else if (score>=0) {
        int loadG1 = generateG1.nextInt(99999)+10000;
        number.setText(""+loadG1);
    }

or

    if (score>=0 && score<3) {
        int loadG1 = generateG1.nextInt(99999)+10000;
        number.setText(""+loadG1);
    }
    else if (score>=3 && score<6){
        int loadG1 = generateG1.nextInt(999999)+100000;
        number.setText(""+loadG1);
    }
    else if (score>=6 && score<10){
        int loadG1 = generateG1.nextInt(9999999)+1000000;
        number.setText(""+loadG1);
    }
    else if (score>=10){
        int loadG1 = generateG1.nextInt(99999999)+10000000;
        number.setText(""+loadG1);
    }
Comments