2

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.

3
  • What about it is "not working"? Be specific. If you have a stack trace, include that, too. Commented Mar 5, 2014 at 22:43
  • 1
    Stack Traces.... We need Stack Traces... Commented Mar 5, 2014 at 22:43
  • 2
    You should initialize every array in your array of arrays. Commented Mar 5, 2014 at 22:44

3 Answers 3

7

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

Corrected code. Look on this. Condition of two fors was fixed. You should do it using length and better is using '<' not '<='. That prevent you from many ArrayIndexOutOfBoundsException .

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<product.length;i++)
            {
                for(int j=0;j<product[i].length;j++){
                    System.out.println(" enter your data");

                    product[i][j]=userInput.nextDouble();

                }
            }
        }

to see the efect you can simply paste down that code.

for (double[] doubles : product) {
                for (double aDouble : doubles) {
                    System.out.println(aDouble);
                }
            }

3 Comments

More than corrected code, you should explain what was wrong, how to fix it and why this fix works.
OutOfArrayExceptions you mean ArrayIndexOutOfBoundsException
Yes ArrayIndexOutOfBoundsException :)

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.