Creating user input for randomized 2D java array issue - OutOfBoundsException - java

Trying to create a 2D array that takes the users input for rows and columns. The numbers inside the array are then randomized from 0 to 100. I am getting the following error:
Enter rows for the array: 3
Enter columns for the array: 2
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
at test2.main(test2.java:17)
Here is my code:
import java.lang.Math;
import java.util.Scanner;
public class test2 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter rows for the array: ");
int rows = scan.nextInt();
System.out.print("Enter columns for the array: ");
int columns = scan.nextInt();
int [][] myArray = new int [rows][columns];
for (rows = 0; rows < myArray.length;rows++) {
for (columns = 0; columns < myArray.length; columns++)
myArray[rows][columns] = (int) (Math.random()*100);
System.out.print(myArray[rows][columns] + "\t");
}
}
}

You should use separate variables to run the loops.
public class Random {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter rows for the array: ");
int rows = scan.nextInt();
System.out.print("Enter columns for the array: ");
int columns = scan.nextInt();
int[][] myArray = new int[rows][columns];
for (int row = 0; row < rows; row++) {
for (int col = 0; col < columns; col++) {
myArray[row][col] = (int) (Math.random() * 100);
System.out.print(myArray[row][col] + "\t");
}
}
}
}

Related

Multidimensional arrays from standard input

I'm trying to figure out how to read in a multidimensional array from standard input given the user provides the row and col size followed by integers of the array
e.g. Input:
2 3 <= row, col of array
8 3 10 < array integers
7 9 6
My code is currently:
int colA = scan.nextInt();
int rowA = scan.nextInt();
int[][] array = new int[rowA][colA];
for (int i = 0; i <rowA;i++){
for (int j=0; j<colA;j++){
array1[i][j] += scan.nextInt();
}
}
And the output of my array is: [[8,3,10,7,9,6]] but what I'd like to do is output [[8,3,10],[7,9,6]]
Here first the row and col value got from users is reversed that is the only mistake i could see
import java.util.Arrays;
import java.util.Scanner;
public class TwoDimensionalArray {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int rowA = scan.nextInt();
int colA = scan.nextInt();
int[][] array = new int[rowA][colA];
for (int i = 0; i < rowA; i++) {
for (int j = 0; j < colA; j++) {
array[i][j] += scan.nextInt();
}
}
for (int[] innerArray : array) {
System.out.println(Arrays.toString(innerArray));
}
}
}
This is a working one
There are some errors in your code.
You inverted the order of the dimensions asked to the users.
Use this:
int rowA = scan.nextInt();
int colA = scan.nextInt();
Instead of this:
int colA = scan.nextInt();
int rowA = scan.nextInt();
You wrote array1 instead of array in array1[i][j] += scan.nextInt();
Note that you can use array[i][j] = scan.nextInt(); instead of array[i][j] += scan.nextInt()

How to take 2 key board inputs to a 2 dimensional array in java?

I want to create a 2D String array which stores name and address which are given as keyboard inputs.
eg: name 1 address 1;
name 2 address 2;
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[][] array = new String[3][2];
for (int i = 0; i < 3; i++)
{
for(int j = 0; j < 2; j++)
{
array[i][j] = sc.nextLine();
}
System.out.println(array[0][0]);
}
}
in here I want to print a statement asking to enter the name
eg. System.out.println("Enter name:");
after that I want to write another statement asking to enter the address
eg.System.out.println("Enter the address");
but i cant figure out how do that in a 2d array
How about something like this?
public static void main(String[] args) {
int rows = 3;
int cols = 2;
Scanner sc = new Scanner(System.in);
String[][] array = new String[rows][cols];
for (int row = 0; row < rows; row++) {
System.out.println("Enter name:");
array[row][0] = sc.nextLine();
System.out.println("Enter the address:");
array[row][1] = sc.nextLine();
}
// Print out array
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
System.out.print(array[row][col] + ", ");
}
System.out.print(";");
}
}
Output:
Person 1, Address 1, ; Person 2, Address 2, ; Person 3, Address 3, ;
Check it live here: https://onlinegdb.com/Byu4wc1vI
I hope this helps!

2D Array to take float inputs from user and calculate sum of columns

I'm really new to Java and have been given a task to create a two dimensional array that takes input from the user. The problem I am having is that it needs to take in some decimal numbers as well as whole numbers then calculate the sum of each column and my program is returning an error once a decimal number is input. I have tried changing all instances of "int" to "float" but still the same error pops up. I also then need to print out the totals of each column.
My code so far is:
package sumElements;
import java.util.Scanner;
public class sumElements{
public static void main(String args[]){
int row, col, i, j;
int arr[][] = new int[3][4];
Scanner scan = new Scanner(System.in);
// enter row and column for array.
row = 3;
col = 4;
// enter array elements.
System.out.println("Enter " +(row*col)+ " array elements row by row (4 per row) : ");
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
arr[i][j] = scan.nextInt();
}
}
// the 2D array is here.
System.out.print("The Array is :\n");
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
System.out.print(arr[i][j]+ " ");
}
System.out.println();
}
}
}
Changed code:
package sumElements;
import java.util.Scanner;
public class sumElements{
public static void main(String args[]){
float row, col, i, j;
float arr[][] = new float[3][4];
Scanner scan = new Scanner(System.in);
// enter row and column for array.
row = 3;
col = 4;
// enter array elements.
System.out.println("Enter " +(row*col)+ " array elements row by row (4 per row) : ");
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
arr[i][j] = scan.nextFloat();
}
}
// the 2D array is here.
System.out.print("The Array is :\n");
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
System.out.print(arr[i][j]+ " ");
}
System.out.println();
}
}
}
package sumElements;
import java.util.Scanner;
public class sumElements{
public static void main(String args[]){
int row, col, i, j;
float arr[][] = new float[3][4];
Scanner scan = new Scanner(System.in);
// enter row and column for array.
row = 3;
col = 4;
// enter array elements.
System.out.println("Enter " +(row*col)+ " array elements row by row (4 per row) : ");
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
arr[i][j] = scan.nextFloat();
}
}
// the 2D array is here.
System.out.print("The Array is :\n");
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
System.out.print(arr[i][j]+ " ");
}
System.out.println();
}
}
Indices must be int since elements of the array float

How to get the sum of each row in java using multidimensional array?

So i'm new at java and I'm currently trying to learn how to use array. So what i'm trying to do is to create a program the will as the user the numbers of rows and column and print the sum of the 2d array.
So the output should be like this:
Enter the number of row:2
Enter the number column:2
Enter a number:1
Enter a number:2
Enter a number:3
Enter a number:4
Output:
1 2 3
3 4 7
Here is my output:
Enter the number of row:2
Enter the number column:2
Enter a number:1
Enter a number:2
Enter a number:3
Enter a number:4
Output:
1 2
3 4
Somehow i cant find a way to sum each row.
Here is my code:
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
int sum = 0;
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of row: ");
int row = input.nextInt();
System.out.print("Enter the number of column: ");
int column = input.nextInt();
int [][] array = new int[row][column];
for(int i = 0; i<row; i++){
for(int j = 0; j<column; j++){
System.out.print("Enter a number: ");
array[i][j] = input.nextInt();
}
}
for(int i = 0; i<row; i++){
for(int j = 0; j<column; j++){
System.out.print(array[i][j]+" ");
}
System.out.println();
}
}
}
I've been trying to do this for the past 3 hours and i cant seem to find a way to sum each row . I'm pretty new at this so any help would be greatly appreciated!
So basically, have a variable initialized for each row to calculate sumof all elements of the given row. Display it after the elements are displayed
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of row: ");
int row = input.nextInt();
System.out.print("Enter the number of column: ");
int column = input.nextInt();
int [][] array = new int[row][column];
for(int i = 0; i<row; i++){
for(int j = 0; j<column; j++){
System.out.print("Enter a number: ");
array[i][j] = input.nextInt();
}
}
for(int i = 0; i<row; i++){
int s = 0; // this variable calculates the sum
for(int j = 0; j<column; j++){
s+=a[i][j]; // summing elements of that row
System.out.print(array[i][j]+" ");
}
System.out.println(s);
}
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);/*initializing the variable input with
new Scanner object*/
System.out.println("Enter the number of rows");/*getting the number of rows
and columns of the 2D array as an user input*/
int noOfRows=input.nextInt();
System.out.println("Enter the number of columns");
int noOfColumns=input.nextInt();
int newArr[][] = new int[noOfRows][noOfColumns];//creation of the 2D array
for(int i = 0 ; i < noOfRows ; i++) {/*Goal of this for loop is to assign the
userInpts to the array indexes*/
for(int j = 0 ; j < noOfColumns ; j++) {
System.out.println("Enter a number");/*Getting the user inputs to
store in the array*/
newArr[i][j] = input.nextInt();
}
}
for(int i = 0 ; i < noOfRows ; i++) {/*Goal of this for loop is printing the
array and sum of the row at the end of the each row*/
int sumofTheRow = 0;/*This is the variable which holds the sum of the
current row so initial value is 0 */
for(int j = 0 ; j < noOfColumns ; j++) {
sumofTheRow += newArr[i][j];/*so inside the inner for loop sumOfTheRow
will be calculated by adding the elements in the row one by one*/
System.out.print(newArr[i][j] + " ");
}
System.out.println(sumofTheRow);//Calculated sum of the row will be displayed
/*So now if i is less than noOfRows then again it will increment the value of i by one and move to the next row */
}
input.close();/*closing the scanner variable to avoid memory leaks --> [ now garbage collector will free the memory area of scanner object by collecting the scanner object]*/
}
}

Adding values on 2d array java

I have created a program that lets the user input the number of rows and columns they want in a 2d array and then it fills the array with all even numbers starting from 0.
I have to add all the numbers in the array to get a total sum and I have no idea how to do that. The rest of my program is complete I'm just having trouble with the sum.
Here is my code:
import java.util.*;
public class ArrayOver {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("How many rows? ");
int x = scan.nextInt();
System.out.println("How many columns? ");
int y = scan.nextInt();
int[][] nums = new int[x][y];
fillArray(nums);
displayArray(nums);
System.out.println();
}
public static void fillArray(int nums[][]) {
int count = 0;
for (int row = 0; row < nums.length; row++) {
for (int col = 0; col < nums[0].length ; col++) {
nums[row][col] = count;
count++;
count++;
}
}
}
public static void displayArray(int nums[][]){
for (int row = 0; row < nums.length; row++) {
System.out.println(Arrays.toString(nums[row]));
}
}
}
Try this:
public static void countArray(int[][] nums)
{
int total=0;
for (int row=0;row<nums.length;row++)
for (int col=0;col<nums[0].length;col++)
total += nums[row][col];
System.out.println(total);
}
This should go through all the numbers in the array and add their values to total.
If you want sum all elements of array, just do it:
int sum = 0;
for(int row = 0; row < nums.length ; row++) {
for (int col = 0; col < nums[row].length ; col++) {
sum = sum + nums[row][col];
}
}

Categories

Resources