Random array Android

0

I have an array in String and I want to extract several values from it and show them in a textview when I press a button but I do not know how to get several data from the array. Here is what I have

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Random r = new Random();
        final String[] elementos = {"R","L","F","B","D","U","R'","L'","F'","B'","D'","U'","R2","L2","F2","B2","U2","D2"};
        final TextView txtScram = (TextView) findViewById(R.id.txtScram);

        Button btnObtener = (Button) findViewById(R.id.btnObtener);
        btnObtener.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               String ranEle = elementos[r.nextInt(elementos.length)];
               txtScram.setText(ranEle);

             }


            }
        });
    }
    
asked by callo33 20.05.2017 в 21:29
source

4 answers

0

You can use a for to run several times your code that takes out the elements

in the following just change numElOut by the number of items you want to take

        public void onClick(View v) {
           String outTxt="";
           int numElOut=12;
           for(int i=0;i<numElOut;i++)
               outTxt += elementos[r.nextInt(elementos.length)];            
           txtScram.setText(outTxt);
         }
    
answered by 20.05.2017 в 21:45
0

The limit of the elements that you want to obtain from your arrangement is defined by you or by the user? Seeing your code everything works well I come out that you only take 1 when you really want 1 or more. I would edit this from your code

Button btnObtener = (Button) findViewById(R.id.btnObtener);
btnObtener.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        StringJoiner joiner = new StringJoiner(",");
        for (int i = 0; i < 3; i++) {
            String ranEle = elementos[r.nextInt(elementos.length)];
            joiner.add(ranEle);
        }
        txtScram.setText(joiner.toString());
    }
}
});

Use StringJoiner to make a separator of each word or element of your arrangement to show it better in TextView . In my example you can get 3 elements.

    
answered by 22.05.2017 в 15:43
0

I would add a method that gives me the amount of random values I want from the array:

private String getRandomValues(String[] valores, int cantidad){
    String res = "";
    for (int i = 0; i < cantidad; i++){
        res += valores[new Random().nextInt(valores.length)] + " ";
    }
    return res;
}

and within onClick() I call it, specifying how many values I want:

   @Override
      public void onClick(View v) {
          String ranEle = getRandomValues(elementos, 5); //5 elementos aleatorios del array.  
          txtScram.setText(ranEle);
      }
    
answered by 22.05.2017 в 23:20
0

You could also use StringBuilder so you can go through the array

StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < path_On.size(); i++) {
                stringBuilder.append(path_On.get(i));
            }
            String newPath = stringBuilder.toString();

and assign newPath to the textview

// I'll add the following example with the ramdom class

Sure, mate look, you can declare the Random class, as the following example that you attach two examples so you can guide you a little better, greetings.

import java.util.Random;
    public class RandomSelect {

        public static void main (String [] args) {

             String [] array = {"A", "B", "C", "D"};
             Random random = new Random();

             int select = random.nextInt(array.length); 

             System.out.println("Random: " + array[select]); 
        }
    }

    Usando charAt:

    import java.util.Random;
    public class RandomSelect {

        public static void main (String [] args) {

             String text = "Hello World";
             Random random = new Random();

             int select = random.nextInt(text.length()); 

             System.out.println("Random char selected: " + text.charAt(select)); 
        }
    }

You could also use chatAt you can add an example of greetings

  public static void main(String[] args) {

            String value = "cat";

            for (int i = 0; i < value.length(); i++) {
                char c = value.charAt(i);
                System.out.println(c);
            }
        }
    
answered by 20.05.2017 в 23:45