With this program I am supposed to use methods to pass through to obtain user input to fill a 3x4 2d array. Then add the sum of the columns and display the results.
The int[][] grid = fillArray(); has an error that in[][] required. Why am I unable to call my method in the main? This is how the book says to do it along witih countless youtube videos.
public class SumOfColumns {
public static int sumColumn(int[][] m, int columnIndex) {
for (int column = 0; column < 4; column++) {
columnIndex = 0;
for (int row = 0; row < 3; row++) {
columnIndex += m[column][row];
}
}
return columnIndex;
}
public static void main(String[] args) {
***int[][] grid = fillArray();***
}
public static int[][] fillArray(int[][] userInput) {
Scanner input = new Scanner(System.in);
int[][] grid = new int[3][4];
System.out.println("Enter a 3x4 grid of numbers please: ");
for (int row = 0; row < grid.length; row++) {
for (int column = 0; column < grid[row].length; column++) {
grid[row][column] = input.nextInt();
}
}
return grid;
}
}
When you declare your fillArray function you gave it an input of int[][] userInput, but then when you call:
int[][] grid = fillArray();
you don't give fillArray and input of int[][]. If you remove the paramter from the fillArray function that should fix it. It would look like:
public static int[][] fillArray() {
// your code...
}
So i'm a beginner;
The task is to convert a given string into the array, the string always has the first characcter as the amount of rows and the second character as the amount of columns.
My problem is to solve how to move the rest of the string 's' into the 2D array from the 1D array.
Thanks in advance!
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String[] s = scanner.nextLine().split(" ");
int arows = Integer.parseInt(s[0]);
int acols = Integer.parseInt(s[1]);
int[][] cells = new int[arows][acols];
for (int col = 0; col < acols; col++){
for (int row = 0; row <= arows;row++){
cells[row][col] = Integer.parseInt(s[2]);
}
}
}
}
You need to implement a counter for your for-loops to iterate through the input string. What you are doing right now is to fill your 2D-Array with the third element of your string.
One solution would be to just declare a variable i = 2, and increment it for each pass of the inner for-loop.
int i = 2
for (int col = 0; col < acols; col++){
for (int row = 0; row < arows;row++){
cells[row][col] = Integer.parseInt(s[i]);
i++;
}
}
Edit: removed <= in row loop, changed the initial value of the index to 2
This is the solution, you have to put another iterator, and initialize it to 2, so to skip the first two elements of s[]
int i = 2;
for (int col = 0; col < acols; col++){
for (int row = 0; row < arows;row++){
cells[row][col] = Integer.parseInt(s[i]);
i++;
}
}
I am trying to make an array that outputs a pattern depending on how many rows and columns I give it for the input and I receive an error when it gets to the third method. I understand that the array begins at index zero and if i input (0 0) for the matrix it's out of bounds but i have no idea how to fix the problem. Thank you for the help!
Here is my code for the first class:
public class Transpose {
public static int [][] createPatterned2DArray(int rows, int cols)
{
int [][] table = new int [rows] [cols];
for (int numRows = 0; numRows < table.length; numRows++){
for (int numCols = 0; numCols < table[0].length; numCols++){
table [numRows][numCols] = 10 + rows*(numRows +1) + numCols;
}
}
return table;
}
public static void print2DArray (int[][] matrix)
{
for (int row = 0; row < matrix.length; row++)
{
for (int col = 0; col < matrix[0].length; col++)
{
System.out.printf("%-4d",matrix[row][col]);
}
System.out.println();
}
}
public static void print2DArrayTransposed(int [][] matrix)
{
for (int row = 0; row < matrix[0].length; row++)
{
for (int col = 0; col < matrix.length; col++)
{
//try {
// if (matrix[0] == 0) {
// System.out.println(matrix[0][0]);
// throw new Exception();
System.out.printf("%-4d",matrix [col][row]);
// }
//catch (Exception e){
// System.out.print(e);
}
System.out.println();
}
}
}
Here is the second class:
import java.util.*;
public class TestTranspose extends Transpose {
public static void main(String[] args)
{
Scanner scan = new Scanner (System.in);
int rows = scan.nextInt();
int cols = scan.nextInt();
int [][] table = createPatterned2DArray(rows,cols);
print2DArray(table);
System.out.println();
print2DArrayTransposed(table);
System.out.println();
}
}
This is the error that I am getting and its driving me insane!
I can't seem to wrap my head around how to throw an exception or to make the output display nothing when i enter an input of (0 0) for the arrays. How can I correct this line of code that does not let me output an array of (0 0)?
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Transpose.print2DArrayTransposed(Transpose.java:32)
at TestTranspose.main(TestTranspose.java:13)
You can't safely do matrix[0].length without first ensuring that matrix.length != 0. In your other two methods, the outer loops on row take care of this.
But in print2DArrayTransposed it's your outer loop that's trying to do row < matrix[0].length; there's nothing to stop it trying to do that even when matrix.length == 0. You can address this in one of two ways: add this early bail-out at the top of print2DArrayTransposed:
if (matrix.length == 0)
return;
or change your outer loop on 'row' to:
for (row = 0; matrix.length > 0 && row < matrix[0].length; ++row)
Take a look at this line of code in the Transpose.print2DArrayTransposed() method:
for (int row = 0; row < matrix[0].length; row++)
You are trying to access the first element of matrix, but there is no element because the length is zero.
A 0x0 array is valid, so no need to disallow creating the array in the first place. My suggestion would just be to simply check at the start of the method if the length of matrix is zero, and if so, just return because there is nothing to print (or print some helpful message instead).
public static void print2DArrayTransposed(final int[][] matrix) {
if (matrix.length == 0) {
return;
}
//...
}
// getting exception in thread main error in result[row][col]= arrayfirst [row][col] + arraysecound [row[col];
public class apples
public static void main(String args[])
{
int arrayfirst[] [] ={{1,2,3},{2,3,4}};
int arraysecound[] [] ={{3,4,5},{6,7,8}};
int result[][]= new int [2][2];
for(int row =0; row<arrayfirst.length; row++)
{
for(int col =0; col<arrayfirst[row].length; col++)
{
result[row][col]= arrayfirst [row][col] + arraysecound [row][col];
}
}
for(int row =0; row<arrayfirst.length; row++) {
for(int col =0; col<arrayfirst[row].length; col++) {
System.out.print(" "+result[row][col]);
}
System.out.println();
}
}
}
// BUT THESE SIMILAR PROGRAMS RUNS CORRECTLY WHY
public static void main(String args[])
{
int arrayA[][] = {{1,4,3,5},{3,8,1,2},{4,3,0,2},{0,1,2,7}};
int arrayB[][] = {{6,1,0,8},{3,2,1,9},{5,4,2,9},{6,5,2,0}};
int arrayC[][] = new int[4][4];
for(int i = 0; i < arrayA.length; i++) {
for(int j = 0; j< arrayA[0].length; j++) {
arrayC[i][j] = arrayA[i][j] + arrayB[i][j];
// System.out.print(arrayC[i][j] + " ");
} // end j for loop
} // end i for loop
for (int i = 0; i < arrayC.length; i++) {
for (int x = 0; x < arrayC[i].length; x++) {
System.out.print(arrayC[i][x] + " | ");
}
System.out.println();
}
} // end main
} // end class
int arrayfirst[] [] ={{1,2,3},{2,3,4}};
int arraysecound[] [] ={{3,4,5},{6,7,8}};
here, arrayfirst and arraysecound contain two rows and three columns each (The number of inner curly braces separated by Comma signify number of rows, and the numbers written within these inner curly braces signify number of columns), so when you add their elements and try to store the result, you again need an array that has two rows and three columns.
so, just replace
int result[][]= new int [2][2];
with
int result[][]= new int [2][3];
Your result array in the first program is too small.
Change following
int result[][]= new int [2][2];
to
int result[][]= new int [2][3];
Also, to learn more about arrays in java, have a look at http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
How would I make a board using the 3d generic array that contains col, row, and the stack (contains 4, 3, 2, 1).
This is what I declared:
private int row, col, stack;
int[][][] array3Dboard = new int[row][col][stack];
I just having trouble how to make an board using the multidimensional.
public void initalized(int arg1){
this.playerID = arg1;
this.array3Dboard = new int[4][4][4];
//create a data structure for the current contents of the board and stacks.
}
Thanks.
You need to make sure the class with method initialized() has int[][][] array3Dboard declared as an instance variable.
You also need to write:
int row = 4;
int col = 4;
int stack = 4;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
for (int k = 0; k < stack; k++) {
array3Dboard[i][j][k] = 0; //replace 0 with some numerical value
}
}
}