dimanche 18 août 2019

How to add exceptions in arraylist in java

I need to add exception so the numbers that already been generated cant be generated again.

So to generate a random number i try this. I make two arraylist with 14 numbers from 1 to 14 in each:

    ArrayList<Integer> Numbers1 = new ArrayList<Integer>();
    ArrayList<Integer> Numbers2 = new ArrayList<Integer>();
    Numbers1.add(0);
    Numbers1.add(1);
    Numbers1.add(2);
    Numbers1.add(3);
    Numbers1.add(4);
    Numbers1.add(5);
    Numbers1.add(6);
    Numbers1.add(7);
    Numbers1.add(8);
    Numbers1.add(9);
    Numbers1.add(10);
    Numbers1.add(11);
    Numbers1.add(12);
    Numbers1.add(13);
    Numbers1.add(14);
    Numbers2.add(0);
    Numbers2.add(1);
    Numbers2.add(2);
    Numbers2.add(3);
    Numbers2.add(4);
    Numbers2.add(5);
    Numbers2.add(6);
    Numbers2.add(7);
    Numbers2.add(8);
    Numbers2.add(9);
    Numbers2.add(10);
    Numbers2.add(11);
    Numbers2.add(12);
    Numbers2.add(13);
    Numbers2.add(14);

then i make a Random that takes 1 number from each arraylist

   Random Calc = new Random();
    Num1 = Numbers1.get(Calc.nextInt(Numbers1.size()));
    Num2 = Numbers2.get(Calc.nextInt(Numbers2.size()));

then i make an integer that multiply the two random numbers that been choosen randomly

    FinalInt = Num1 * Num2;

then i have an HashMap with 200 Strings:

 HashMap<Integer, String> points = new HashMap<Integer, String>();
    points.put(1, "String1");
    points.put(2, "String2");
    points.put(3, "String3");
    points.put(4, "String4");
    points.put(5, "String5");
    //And so on until 200 
    TextView textview1.setText(points.get(FinalInt))

If the FinalInt generate number 1 it will set textview1 to "String1". So i need if FinalInt is 1 again to redo num1 * num2 so its not 1.




Aucun commentaire:

Enregistrer un commentaire