-1

Trying to change this code so I can get user input instead of a predefined array. Can anyone help? This code needs to have the method sum with the double[][] parameter. The method should return the sum of the elements of the array passed to it and should be rectangular.

import java.util.Scanner;

class Array2_1
{
    public static double sum (double[][] array)
    {
        double sum = 0;
        for (int i = 0; i < array.length; i++)
        {
            for (int j = 0; j < array[0].length; j++)
            {
                sum += array[i][j];
            }
        }
        return sum;
    }

    public static void main(String[] args)
    {
        System.out.println("")
        double a [][] = {
                {1.2, 2},
                {6, 7.2},
                {11, 12}
        };
        System.out.println(sum(a));
    }
}
waddles313
  • 21
  • 3
  • There are many questions about reading user input in Java. For example, see [this question](https://stackoverflow.com/questions/11871520/how-can-i-read-input-from-the-console-using-the-scanner-class-in-java) or [this one](https://stackoverflow.com/questions/15446689/what-is-the-use-of-system-in-read). – Bobulous Feb 27 '21 at 16:24
  • Will the array always be 2x2, or should the user be able to choose the dimensions? – Kevin Anderson Feb 27 '21 at 16:26
  • Copy the entire method, `sum()`, and give it a new name like `getUserInput()`. Then change the `sum += array[i][j];` line so it gets a double from the user (see examples from the links provided by Bobulous). In `main()`, call your your new `getUserInput()` method, passing in the array, before calling `sum()`. – Idle_Mind Feb 27 '21 at 16:42

2 Answers2

2

You can use Scanner to read values from the keyboard. First, read the values for the dimensions of the array and then input values for the individual indices using nested loops.

You can also define a method to display the array in the tabular form.

Demo:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the number of rows: ");
        int rows = scanner.nextInt();
        System.out.print("Enter the number of columns: ");
        int cols = scanner.nextInt();

        double array[][] = new double[rows][cols];

        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.printf("Enter value for array[%d][%d]: ", i, j);
                array[i][j] = scanner.nextDouble();
            }
        }

        System.out.println("The array:");
        print(array);

        System.out.println("Sum of elements: " + sum(array));
    }

    public static double sum(double[][] array) {
        double sum = 0;
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                sum += array[i][j];
            }
        }
        return sum;
    }

    public static void print(double[][] array) {
        for (double[] row : array) {
            for (double col : row) {
                System.out.print(col + "\t\t");
            }
            System.out.println();
        }
    }
}

A sample run:

Enter the number of rows: 3
Enter the number of columns: 2
Enter value for array[0][0]: 1.2
Enter value for array[0][1]: 2
Enter value for array[1][0]: 6
Enter value for array[1][1]: 7.2
Enter value for array[2][0]: 11
Enter value for array[2][1]: 12
The array:
1.2     2.0     
6.0     7.2     
11.0    12.0        
Sum of elements: 39.4
Arvind Kumar Avinash
  • 50,121
  • 5
  • 26
  • 72
0

I have added comments to the code for your understanding. Hope it answers your query.

public static void main(String[] args) {

    //You can use the Scanner class for taking input
    Scanner scan = new Scanner(System.in);

    //take input for the number of rows in array
    System.out.println("Enter no. of rows in array");
    int row = scan.nextInt();

    //take input for the number of columns in array
    System.out.println("Enter no. of columns in array");
    int col = scan.nextInt();

    //create an array with row and col as dimensions of the 2D array
    double[][] arr = new double[row][col];

    //this for loop is for inputting elements in your array
    for (int i = 0; i < row; i++)
        for (int j = 0; j < col; j++)
            arr[i][j] = scan.nextInt();

    //this for loop is for printing out the elements 
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++)
            System.out.print(arr[i][j] + " ");
        System.out.println();
    }
    scan.close();

    //now give a call to your sum() method and print the result
    System.out.println(sum(arr)); 
}
neha
  • 683
  • 4
  • 21