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

7
  • 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? Commented Apr 19, 2017 at 18:01
  • int[] j = new int[i]; this creates a brand new array, so, you are writing over your previous value. Commented Apr 19, 2017 at 18:01
  • I suggest some reading: docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html Commented Apr 19, 2017 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 ith 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. Commented Apr 19, 2017 at 18:03
  • your array length should be i+1 to set value at index i Commented Apr 19, 2017 at 18:05

2 Answers 2

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++) {
...
Sign up to request clarification or add additional context in comments.

4 Comments

that's why i made j, k, l and m, j is saving all values from a when generating their randoms while i is increasing.
Example, j is saving all values from A until i gets to the sixth iteration. k is saving all values from B until i gets to the sixth iteration, etc
@RodrigoRSMahecha I've fixed my answer according to your comments.
It Worked! Thanks a lot for ur help and ur time ^^ i appreciate it
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);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.