I can not initialize a two dimensional array using for loop
import java.util.Scanner;
public class Quater{
public static void main(String[] args){
//declare an array
double product[][]=new double[3][2];
//declare a Scanner object
Scanner userInput=new Scanner(System.in);
//ask the user for input
System.out.println("Please enter your data");
for(int i=0;i<=1;i++)
{
for(int j=0;j<=2;j++){
System.out.println(" enter your data");
product[i][j]=userInput.nextDouble();
}
}
}
}
The problem is when I try to enter the third number, This shut down and not working, however, if I make it new double[100][100], this can work and allow me to enter 6 number.
The outer cycle iterates over rows and inner over columns, therefore you need to have an array of dimensions double[2][3], not double[3][2] that you use.
If you make your array new double[3][2], then you can access product[i][j] where i goes up to 3-1=2, and j goes up to 2-1=1. That is, the bounds of the indexes in the same order in which they appear in the new. But you're trying to access product[i][j] where j is 2.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With