3

I want to change the code, so that when you click the "next number" button each time, it generates a random number. When generating the last number, the app will do the sorting except the last number. After generating the last number, click the button will have no response. You need to click "clear" button to restart. And no repetition is appeared. Here is my code MainActivity.java

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

int Count=0;
public void generate(View v){
    Random myRandom = new Random();

    TextView tv_number_one = (TextView)findViewById(R.id.tv_number_one);
    TextView tv_number_two = (TextView)findViewById(R.id.tv_number_two);
    TextView tv_number_three = (TextView)findViewById(R.id.tv_number_three);
    TextView tv_number_four = (TextView)findViewById(R.id.tv_number_four);
    TextView tv_number_five = (TextView)findViewById(R.id.tv_number_five);
    TextView tv_number_six = (TextView)findViewById(R.id.tv_number_six);
    TextView tv_number_seven = (TextView)findViewById(R.id.tv_number_seven);
    String ran=String.valueOf(myRandom.nextInt(50));
    switch(Count%7){

        case 0:
            tv_number_one.setText(ran);
            break;

        case 1:
            tv_number_two.setText(ran);
            break;

        case 2:
            tv_number_three.setText(ran);
            break;

        case 3:
            tv_number_four.setText(ran);
            break;

        case 4:
            tv_number_five.setText(ran);
            break;

        case 5:
            tv_number_six.setText(ran);
            break;

        case 6:
            tv_number_seven.setText(ran);
            break;

    }
    Count++;
}
public void clear (View v){
TextView tv_number_one = (TextView)findViewById(R.id.tv_number_one);
TextView tv_number_two = (TextView)findViewById(R.id.tv_number_two);
TextView tv_number_three = (TextView)findViewById(R.id.tv_number_three);
TextView tv_number_four = (TextView)findViewById(R.id.tv_number_four);
TextView tv_number_five = (TextView)findViewById(R.id.tv_number_five);
TextView tv_number_six = (TextView)findViewById(R.id.tv_number_six);
TextView tv_number_seven = (TextView)findViewById(R.id.tv_number_seven);

tv_number_one.setText("?");
tv_number_two.setText("?");
tv_number_three.setText("?");
tv_number_four.setText("?");
tv_number_five.setText("?");
tv_number_six.setText("?");
tv_number_seven.setText("?");

}

}

activity_main

<TextView android:text="\?" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tv_number_one"
    android:textSize="30sp"
    android:textIsSelectable="true"
    android:layout_marginLeft="10dp"
    android:textColor="#67ceff"
    android:layout_marginStart="20dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="\?"
    android:id="@+id/tv_number_two"
    android:layout_alignBottom="@+id/tv_number_one"
    android:layout_toRightOf="@+id/tv_number_one"
    android:layout_toEndOf="@+id/tv_number_one"
    android:textColor="#67ceff"
    android:textSize="30sp"
    android:layout_marginLeft="20dp"
    android:layout_marginStart="20dp"
    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="\?"
    android:id="@+id/tv_number_three"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/tv_number_two"
    android:layout_toEndOf="@+id/tv_number_two"
    android:layout_marginLeft="20dp"
    android:textColor="#67ceff"
    android:textSize="30sp"
    android:layout_marginStart="20dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="\?"
    android:id="@+id/tv_number_four"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/tv_number_three"
    android:layout_toEndOf="@+id/tv_number_three"
    android:textColor="#67ceff"
    android:textSize="30sp"
    android:textIsSelectable="true"
    android:layout_marginLeft="20dp"
    android:layout_marginStart="20dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="\?"
    android:id="@+id/tv_number_five"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/tv_number_four"
    android:layout_toEndOf="@+id/tv_number_four"
    android:layout_marginLeft="20dp"
    android:textColor="#67ceff"
    android:textSize="30sp"
    android:layout_marginStart="20dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="\?"
    android:id="@+id/tv_number_six"
    android:layout_alignBottom="@+id/tv_number_five"
    android:layout_toRightOf="@+id/tv_number_five"
    android:layout_toEndOf="@+id/tv_number_five"
    android:textColor="#67ceff"
    android:textSize="30sp"
    android:layout_marginLeft="20dp"
    android:layout_marginStart="20dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="\?"
    android:id="@+id/tv_number_seven"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/tv_number_six"
    android:layout_toEndOf="@+id/tv_number_six"
    android:layout_marginLeft="20dp"
    android:textColor="#6198ff"
    android:textSize="30sp"
    android:layout_marginStart="20dp"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="NEXT NUMBER"
    android:id="@+id/button_next_num"
    android:layout_below="@+id/tv_number_one"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="41dp"
    android:onClick="generate"/>

Does anyone know how to do it?

Joey Li
  • 45
  • 5

2 Answers2

2

just add the code in your onclickListener event of button and set counter integer in onclickListener when count reaches 7 then stop. The code is as below-

public class MainActivity extends AppCompatActivity {
int intCount=0;
int[] arr = new int[7];
final Button nextButton;
Random randomGenerator = new Random(); 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    nextButton = (Button)findviewbyid(R.id.button_next_num)
    TextView tv_number_one = (TextView)findViewById(R.id.tv_number_one);
    TextView tv_number_two = (TextView)findViewById(R.id.tv_number_two);
    TextView tv_number_three = (TextView)findViewById(R.id.tv_number_three);
    TextView tv_number_four = (TextView)findViewById(R.id.tv_number_four);
    TextView tv_number_five = (TextView)findViewById(R.id.tv_number_five);
    TextView tv_number_six = (TextView)findViewById(R.id.tv_number_six);
    TextView tv_number_seven = (TextView)findViewById(R.id.tv_number_seven);

    button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click   


             intCount++; //when intCount becomes 7 stop execution
             arr[intCount]= randomGenerator.nextInt(100) + 1;;
               // intCount++; //when intCount becomes 7 stop execution
             if(intCount == 1){tv_number_one.setText(arr[intCount]);}
             else if(intCount == 2){tv_number_two.setText(arr[intCount]);}
             else if(intCount == 3){tv_number_three.setText(arr[intCount]);}
             else if(intCount == 4){tv_number_four.setText(arr[intCount]);}
             else if(intCount == 5){tv_number_five.setText(arr[intCount]);}
             else if(intCount == 6){tv_number_six.setText(arr[intCount]);}
             else if(intCount == 7){tv_number_seven.setText(arr[intCount]);}
             else{ intCount=0;nextButton.setEnabled(false);}
            }
        });    
  }
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
  }

   @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}
sud
  • 505
  • 1
  • 4
  • 12
  • The value changed at count++ is never used. How can I do? – Joey Li Oct 24 '15 at 07:32
  • the intCount is nothing but a integer variable which you can add inside your class and before your onCreat() methode as- public class MainActivity extends AppCompatActivity {int intCount=0; – sud Oct 24 '15 at 07:51
  • hey JOEY comment if this code works for you...use this code it works definitely just copy paste as it is – sud Oct 24 '15 at 08:53
  • Sorry for the late reply. I was worked during past few hours. This works, thank you. But how to stop generating when 7 numbers are generated? And I want to know how to generate with no repetition and sort them when the last number is generated. – Joey Li Oct 24 '15 at 16:48
  • when count becomes 7 just disable the button by- nextButton.setEnabled(false); – sud Oct 27 '15 at 06:01
  • To stop it from generating repeated random numbers refer the given link below- [link] (http://stackoverflow.com/questions/4040001/creating-random-numbers-with-no-duplicates) – sud Oct 27 '15 at 06:07
  • i have made changes in above code as you want check the code again. – sud Oct 27 '15 at 06:17
0

Yes your code will not run because your created an array arr of size 2 but your trying to get 7 elements in that array, it will throw an exception called ArrayIndexOutOfBound. Because declared array having 2 elements trying to retrieve more elements. Why can't to get direct random values and add to textviews.

int clickCount = 0;

public void generate(View v) {
    Random myRandom = new Random();

    if(clickCount >= 7) return;

    TextView tv_number_one = (TextView)findViewById(R.id.tv_number_one);
    TextView tv_number_two = (TextView)findViewById(R.id.tv_number_two);
    TextView tv_number_three = (TextView)findViewById(R.id.tv_number_three);
    TextView tv_number_four = (TextView)findViewById(R.id.tv_number_four);
    TextView tv_number_five = (TextView)findViewById(R.id.tv_number_five);
    TextView tv_number_six = (TextView)findViewById(R.id.tv_number_six);
    TextView tv_number_seven = (TextView)findViewById(R.id.tv_number_seven);

    String randStr = String.valueOf(myRandom.nextInt(48));

    switch(clickCount % 7) {
        case 0:
            tv_number_one.setText(randStr);
            break;

        case 1:
            tv_number_two.setText(randStr);
            break;

        case 2:
            tv_number_three.setText(randStr);
            break;

        case 3:
            tv_number_four.setText(randStr);
            break;

        case 4:
            tv_number_five.setText(randStr);
            break;

        case 5:
            tv_number_six.setText(randStr);
            break;

        case 6:
            tv_number_seven.setText(randStr);
            break;
    }

    clickCount++;
}

And one more thing, String.valueOf() method will return String, so you no need to append an empty string before that.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Jitendra Kumar. Balla
  • 1,173
  • 1
  • 9
  • 15
  • I have changed the array size, thank you. But if I just delete String.valueOf(), will it works? – Joey Li Oct 24 '15 at 07:39
  • Replace my function with your function, it will work – Jitendra Kumar. Balla Oct 24 '15 at 07:41
  • Thanks for your help. But I want each time the user taps "NEXT NUMBER" button, it generates a random number. I have no idea about how to do that. – Joey Li Oct 24 '15 at 07:52
  • Yeah it will generate 7 random number's and placed in corresponding textviews when user will click the button, After replace your code with mine and run it once!. Let me know the status. – Jitendra Kumar. Balla Oct 24 '15 at 08:00
  • But the result is 7 random numbers are generated at the same time. I want to generate 1 number by click the button once. – Joey Li Oct 24 '15 at 08:06
  • @JoeyLi Updated my ans, replace my code generate() with your code and add int clickCount=0; this line. Let me know if is solved. – Jitendra Kumar. Balla Oct 24 '15 at 08:15
  • Just one textview is working. Other six textView have no reaction – Joey Li Oct 24 '15 at 08:34
  • Are you kidding me? You have to click the button then it will filled number, according to your previous comment. – Jitendra Kumar. Balla Oct 24 '15 at 09:44
  • Oh, it's ok now. I think I typed something wrong before. Thank you very much. Sorry, I was working during the past few hours. – Joey Li Oct 24 '15 at 14:50
  • I also want to ask how to stop generating when 7 numbers are generated? And I want to know how to generate with no repetition and sort them when the last number is generated. – Joey Li Oct 24 '15 at 16:51
  • Updated my answer, Mark it solved. Once your problem solved. – Jitendra Kumar. Balla Oct 25 '15 at 06:12
  • I want when generating the last number, the app will do the sorting except the last number. After generating the last number, click the button will have no response. You need to click "clear" button to restart. And no repetition is appeared. How to correct it? – Joey Li Oct 25 '15 at 06:29
  • For sorting, Is it better to use array? – Joey Li Oct 25 '15 at 06:29
  • Your not clear with your question. Make sure first your clear. We(S.O) are not your clients. Don't post your requirements in S.O. First tell what is your problem. – Jitendra Kumar. Balla Oct 25 '15 at 12:41