0

This is an UI that shows up random values with a JTable. It works fine all the math procediments but when I apply them to the JTable, it throws me an ArrayIndexOutOfBoundsException. ¿Could yout tell me what's going wrong and how to solve it? Finally, I have to thank you for spending time in search of this malfunctioning.

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class RandomVals extends JFrame {

    JTable a;
    JScrollPane b;

    public RandomVals () {
      String [] header = new String[15];                 //Fulfills 2D array
        for (int i = 0; i < header.length; i++) {        //with random numbers
            header[i]=String.valueOf(i);
        }

      String [][] rows = new String[15][8];
        for (int i = 0; i < rows.length; i++) {
          System.out.println();
            for (int j = 0; j < 8; j++) {
                rows [i][j] = "Hi";
                double foo = (Math.random()*2)+8;
                rows [i][j] = String.format("%.2f",foo);
                  System.out.print(rows[i][j]+" ");
            }
        }
        /////////////  All above works but then an exception occurs //////////////

        a = new JTable(rows, header);  //Creating JTable
          b = new JScrollPane(a);
          b.setBounds(10,10,500,500);
        add(b);
    }

    public static void main(String[] args) {  //JFrame build up
        RandomVals e = new RandomVals();
          e.setSize(520,520);
          e.setLocationRelativeTo(null);
          e.setVisible(true);
          e.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Steve
  • 19
  • 7
  • You say your header's got 15 columns, but your row data only has 8 ... do you have your loops around the wrong way? Ie, 8 rows of 15 columns, instead of the 15 rows of 8 columns you're setting up? – MadProgrammer Oct 10 '19 at 00:55
  • Anil M it's the same case but different algorith so it's not. – Steve Oct 10 '19 at 01:15

1 Answers1

2

You're creating a header with 15 columns...

String[] header = new String[15];                 //Fulfills 2D array
for (int i = 0; i < header.length; i++) {        //with random numbers
    header[i] = String.valueOf(i);
}

But then creating row data with 15 rows and 8 columns

String[][] rows = new String[15][8];
for (int i = 0; i < rows.length; i++) {
    System.out.println();
    for (int j = 0; j < 8; j++) {
        if (false) {
            rows[i][j] = "Hi";
        } else {
            double foo = (Math.random() * 2) + 8;

            rows[i][j] = String.format("%.2f", foo);

            System.out.print(rows[i][j] + " ");
        }
    }
}

Changing the above to something more like...

String[][] rows = new String[8][15];
for (int i = 0; i < rows.length; i++) {
    System.out.println();
    for (int j = 0; j < 15; j++) {
        if (false) {
            rows[i][j] = "Hi";
        } else {
            double foo = (Math.random() * 2) + 8;

            rows[i][j] = String.format("%.2f", foo);

            System.out.print(rows[i][j] + " ");
        }
    }
}

seems to work for me

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366