1

Okay, so this program... I have no idea how to make it, let alone make it work. The goal of this program is to accept user INT values to be put into a 2d array (5 rows, 4 columns), output the array, and give the average value of each row. Any help on making it work and shortening it would be greatly appreciated.

import java.io.*; class largeArray { public static void main(String[] args) throws IOException { InputStreamReader inStream = new InputStreamReader (System.in); BufferedReader userInput = new BufferedReader (inStream); System.out.println("Welcome to the array making program."); System.out.println("Please enter the first 4 numbers:"); int[][] datArray = new int[5][4]; datArray[0][0] = Integer.parseInt(userInput.readLine()); datArray[0][1] = Integer.parseInt(userInput.readLine()); datArray[0][2] = Integer.parseInt(userInput.readLine()); datArray[0][3] = Integer.parseInt(userInput.readLine()); System.out.println("Please enter in the next 4 numbers:"); datArray[1][0] = Integer.parseInt(userInput.readLine()); datArray[1][1] = Integer.parseInt(userInput.readLine()); datArray[1][2] = Integer.parseInt(userInput.readLine()); datArray[1][3] = Integer.parseInt(userInput.readLine()); System.out.println("Please enter in the third set of 4 numbers:"); datArray[2][0] = Integer.parseInt(userInput.readLine()); datArray[2][1] = Integer.parseInt(userInput.readLine()); datArray[2][2] = Integer.parseInt(userInput.readLine()); datArray[2][3] = Integer.parseInt(userInput.readLine()); System.out.println("Please enter in the fourth set of numbers:"); datArray[3][0] = Integer.parseInt(userInput.readLine()); datArray[3][1] = Integer.parseInt(userInput.readLine()); datArray[3][2] = Integer.parseInt(userInput.readLine()); datArray[3][3] = Integer.parseInt(userInput.readLine()); System.out.println("Please enter in the last set of numbers:"); datArray[4][0] = Integer.parseInt(userInput.readLine()); datArray[4][1] = Integer.parseInt(userInput.readLine()); datArray[4][2] = Integer.parseInt(userInput.readLine()); datArray[4][3] = Integer.parseInt(userInput.readLine()); for(int row = 0; row < datArray.length; row++) { System.out.print("Row " + row + ": "); for (int column = 0; column < datArray[row].length; column++) System.out.print(datArray[row][column] + " "); System.out.println( ); } } } 

Current issue: How do I get all values in the row to average out in a short hand way? IT also needs to output the average.

2
  • 1
    your array dimensions are wrong. the second dimension has a length of four (index: 0,1,2,3), but in line 17 you address index 4 which does not exist and produces the arrayindexoutofboundsexception. Commented Jun 3, 2013 at 12:07
  • 1
    You're definitely going to want to put those input prompts in loops. Commented Jun 3, 2013 at 12:09

6 Answers 6

5

If you want the arrays to hold 5 objects, use:

int[][] datArray = new int[5][5]; 

Your error is that you set the length to:

int[][] datArray = new int[5][4]; 

So all internal arrays are of length 4 (index 0-3) and you cannot set value at index 4 in:

datArray[0][4] = Integer.parseInt(userInput.readLine()); 

BTW, I suggest you use loops here...

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

3 Comments

No no, it's a 20 array grid, not 25.
So you cannot put the fifth element in it.
Yeah, I caught that as soon as you mentioned it, now the problem is averaging each row out.
3

If you define int[][] datArray = new int[5][4]; you can't use the position datArray[x][4], it is out of bounds. You are asking for 25 number instead of the 20 you want, each row of numbers have the position 0,1,2,3,4 and it should be 0,1,2,3.

To calculate the avg of each row you can:

float[] avg = new float[5]; for(int i=0 ; i < 5 ; i++) { float sum = 0; for(int j=0 ; j < 4 ; j++) { sum += datArray[i][j]; } avg[i] = sum/4; } 

You can add this info to the output message this way:

for(int row = 0; row < datArray.length; row++) { System.out.print("Row " + row + ": "); for (int column = 0; column < datArray[row].length; column++) System.out.print(datArray[row][column] + " "); System.out.print("Row Average value: " + avg[row]); System.out.println( ); } 

8 Comments

Yeah, I caught that as soon as it was mentioned. But now I need to find a shorthand way to average out each row...
You want to calculate the average value of each row?
Yes. I need to do it for each row ([0][0] through [0][4] etc etc)
Uh... Kind of. I need it to be have an output, and its either I'm brain dead, or I simply cannot figure out how to use this code to output the averages of each row.
The same way you did to the datArray. The avg[x] is the avg of the datArray[x][0] to [x][3] values.
|
1

A shorter version:

import java.io.*; class LargeArray { /** * Arrays dimension. */ private static final int ARRAY_DIM_X = 4; private static final int ARRAY_DIM_Y = 5; public static void main(String[] args) throws IOException { BufferedReader tempReader = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Welcome to the array making program."); int[][] tempDatArray = new int[ARRAY_DIM_Y][ARRAY_DIM_X]; for (int x = 0; x < ARRAY_DIM_Y; x++) { System.out.println(String.format("Please enter %1$s numbers:", ARRAY_DIM_X)); for (int y = 0; y < ARRAY_DIM_X; y++) { tempDatArray[x][y] = Integer.parseInt(tempReader.readLine()); } } printArray(tempDatArray); } private static void printArray(int[][] anArray) { for (int row = 0; row < anArray.length; row++) { System.out.print("Row " + row + ": "); for (int column = 0; column < anArray[row].length; column++) { System.out.print(anArray[row][column] + " "); } // now calculate the avrg ... System.out.println(" = " + calculateAverage(anArray[row])); } } private static float calculateAverage(int[] anArray) { float tempSum = 0f; for (int i = 0; i < anArray.length; i++) { tempSum += anArray[i]; } return tempSum / anArray.length; } } 

4 Comments

I.. This level of coding, admittedly, is one that I can't really follow. We weren't taught some of the functions in this coding yet, so I'm a bit shady about it.
Oh yes, you can follow! You used already a loop (for) in your example to print the array out, I do the same for the input, nothing more nothing less ;)
Ah, I forget to mention, the example now print out the average for each row: Row 0: 1 2 3 4 = 2.5 Row 1: 5 6 4 3 = 4.5 Row 2: 1 1 1 1 = 1.0 Row 3: 3 4 3 4 = 3.5 Row 4: 5 6 7 7 = 6.25
To be 100% honest, that block of coding was copied from my notes basically line for line.
0

You have declared int[][] datArray = new int[5][4] which means a two dimensional array with 5 rows and 4 columns and you are insertin at datArray[0][4] which points to 1st row and 5th column hence this exception ,change this to int[][] datArray = new int[5][5] and it will work

Comments

0

To make it clear:

int[][] datArray = new int[5][4]; 

It goes from dataArray[0][0] to dataArray[4][3];

[5][4] means that you take first 5 digit and first 4 digit, which in Java means it goes from 0 to 4 and 0 to 3. In Java first number starts from 0, not 1.

Comments

0

Array starts from index 0 not 1 . In this code, int[][] datArray = new int[5][4]; you are bounding to 5 rows and 4 columns. but you are try for 5th column where the index is not available. So it throws ArrayIndexOutOfBoundsException .

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.