Rodrigo RS Mahecha
Rodrigo RS Mahecha

Reputation: 3

Arrays not saving values JAVA

hope you all are having a good day.

I've got a problem with my code, i am trying to save some values to an array, the array position gets moved by a counter, but when i try to save the values, it displays an error on the console.

this is the code.

        if(i<6){
        i++;
        jTxtEstrategia.setText(String.valueOf(i));


            A = (Math.random() * 8);
            B = (Math.random() * 8);
            C = (Math.random() * 8);
            D = (Math.random() * 8);

            jTxtA.setText(""+(int)A);
            jTxtB.setText(""+(int)B);
            jTxtC.setText(""+(int)C);
            jTxtD.setText(""+(int)D);



            int[] j = new int[i];
            int[] k = new int[i];
            int[] l = new int[i];
            int[] m = new int[i];

            j[i]=(int) A;
            k[i]=(int)B;
            l[i]=(int)C;
            m[i]=(int)D;

            System.out.println("Estrategia "+i+"\n Sucursal A: "+j[i]+"\n Sucursal B: "+k[i]+"Sucursal C: "+l[i]+"\n Sucursal C: "+m[i]);
    }else{
        jButtCalc.setEnabled(false);
    }

The objetive is to save those values to save some code and compare them later on.

I appreciate your time,

Regards (sorry for my bad english).

Upvotes: 0

Views: 1283

Answers (2)

B Gunasekara
B Gunasekara

Reputation: 34

in your code it create array objects again and again, And your array size grow up , so use ARRAYLIST it allow you to generate it's size. you have to use arrayListName.add(index,value) insted of arrayName[index] = value to add value into array list and to retrieve value you have to use arayListName.get(index) insted of using arrayName[index]

    ArrayList<Integer> j= new ArrayList<Integer>();
    ArrayList<Integer> k= new ArrayList<Integer>();
    ArrayList<Integer> l= new ArrayList<Integer>();
    ArrayList<Integer> m= new ArrayList<Integer>();

if(i<6){

        i++;
        jTxtEstrategia.setText(String.valueOf(i));


            A = (Math.random() * 8);
            B = (Math.random() * 8);
            C = (Math.random() * 8);
            D = (Math.random() * 8);

            jTxtA.setText(""+(int)A);
            jTxtB.setText(""+(int)B);
            jTxtC.setText(""+(int)C);
            jTxtD.setText(""+(int)D);



            j.add(i,(int)A);
            k.add(i,(int)B);
            l.add(i,(int)C);
            m.add(i,(int)D);

            System.out.println("Estrategia "+i+"\n Sucursal A: "+j.get(i)+"\n Sucursal B: "+k.get(i)+"Sucursal C: "+l.get(i)+"\n Sucursal C: "+m.get(i));
    }else{
        jButtCalc.setEnabled(false);
    }

Upvotes: 0

radoh
radoh

Reputation: 4809

I am assuming you are getting ArrayOutOfBounds exception?
Let's say value of i is 1. Then you are creating an array of size 1, but accessing its 2nd element, here:

            int[] j = new int[i];
...
            j[i]=(int) A;

You need to define the arrays before the loop/cycle/for/while with a correct number of elements. E.g.:

int[] j = new int[N]; // where N is 6, or some other number?...
 ...
while (...) {
  if(i<6){
    i++;
 ...

But ideally, you should define your loop in a more common way, like:

for (int i = 0; i < 6; i++) {
...

Upvotes: 1

Related Questions