I have a doubt. I am developing the following code which will be a multplication table for a number that you manually introduce. What I cannot get is to print the table. I don't know what is going on because as far as I know, all the code is right written.
public class Tabla
{
public static void main (String[] args)
{
int n=4;
Tabla table = new Tabla ();
int dato [];
dato=table.producto(n);
for (int j=1;j<=10;j++)
{System.out.println(dato[j]);}
}
public int [] producto(int num)
{
int a[]={'0'};
for (int i=1;i<=10;i++)
{a[i]=num*i;}
return a;
}
}
Any ideas??
Thanks in advance!
**I changed the code to:
public class Tabla
{
public static void main (String[] args)
{
int n=4;
int j;
Tabla table = new Tabla ();
int dato[]=new int [10];
dato=table.producto(n);
for (j=0;j<10;j++)
{System.out.println(dato[j]);
}
}
public int [] producto(int num)
{
// make a 10-element array
int a[] = new int[10];
// fill up the array with products
for (int i = 0; i < 10; i++)
{a[i] = num * (i+1); }
return a;
}
}
Works like a charm!
Now I am wondering why the compiler threw the "ArrayIndexOutOfBoundsException" when I had the for cycle as for (int i = 1; i <=10; i++)
Thanks for the help! :D