0

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).

  • 4
    You should include the error you get on the console (Exception) in your question. See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors) – OH GOD SPIDERS Apr 19 '17 at 18:01
  • `int[] j = new int[i];` this creates a brand new array, so, you are writing over your previous value. –  Apr 19 '17 at 18:01
  • I suggest some reading: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html –  Apr 19 '17 at 18:02
  • 2
    Aside from writing over previous values by creating new arrays in each iteration, you're declaring each new array with length `i` and then trying to set the `i`th variable which isn't possible. For example, if you create an array with length 8 then it has indexes 0-7.. there is no 8th index. – mrogers Apr 19 '17 at 18:03
  • your array length should be `i+1` to set value at index `i` – Raghav Apr 19 '17 at 18:05
  • i am sorry, the exception is: – Rodrigo RS Mahecha Apr 19 '17 at 18:15
  • Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1 – Rodrigo RS Mahecha Apr 19 '17 at 18:15

2 Answers2

1

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++) {
...
radoh
  • 4,554
  • 5
  • 30
  • 45
0

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);
    }