Results 1 to 9 of 9
Thread: 2D array
- 10-29-2010, 01:58 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 6
- Rep Power
- 0
2D array
Hello guys..
I am new to jave and looking for some help...
// here is the application
import javax.swing.JOptionPane;
public class rainfall
{
public static void main(String args[])
{
//the first array 4 weeks
int a[][]=new int[4][]; //the user should determine the second array which is the reading of rainfall amount per week.
int rainfall_per_week, reading,total_rain_month=0, avg_rainfall_per_week;
int i,j,k; //loops
for(i=0;i<a.length;i++){
JOptionPane.showMessageDialog(null,"Week "+(i+1));
rainfall_per_week=Integer.parseInt(JOptionPane.sho wInputDialog("Enter the number of rainfall for week "+(i+1)+" "));
for(j=0; j<rainfall_per_week; j++){
reading=0;
k=0;
//reset k and reading to 0
while(k<rainfall_per_week){ //quit when the user enters the amount in the required cols
reading=Integer.parseInt(JOptionPane.showInputDial og("Enter the reading for " + (j+1)));
reading++;
k++;
}
a[i][j]=reading; //asssign the total rainfall per week to row i col j
avg_rainfall_per_week=a[i][j]/rainfall_per_week; //calculate the average per week
total_rain_month+=a[i][j]; //calculate the total amount for the whole month
// break;
}
}
for(i=0;i<a.length;i++){
for(j=0;j<a[i].length;j++){
JOptionPane.showMessageDialog(null,"Per Week "+a[i][j]);
}
JOptionPane.showMessageDialog(null,"The average rainfall for week " +(i+1)+" :");
}
JOptionPane.showMessageDialog(null,"The total rainfall for the whole month is :"+total_rain_month);
}
}
i can run it but the prog terminates after the while loop...
Error: Exception in thread "main" java.lang.NullPointerException
at rainfall.main(rainfall.java:44)
Thanks :)
-
There's a variable on line 44 that has not been initialized (we don't know which line is 44 in your compiled program, but you do). Check all the variables on that line, and you'll see which one is null, then backtrack through the program to be sure that you've initialized all variables.
Luck!
- 10-29-2010, 04:19 AM #3
My head hurts trying to figure out what stuff is doing due to your formatting O.o
Sincerely, Joshua Green
Please REP if I help :)
- 10-29-2010, 06:35 AM #4
Member
- Join Date
- Oct 2010
- Posts
- 6
- Rep Power
- 0
Sorry about the formating...
All I am trying to do is to create two-dimensional array and allow the user to input the number of colums, do some calculation inside a loop then store the total to the array
have a look in this part
Java Code://number of rows 4 int a[][]=new int[4][]; //let the user determine number of columns. int rainfall_per_week, reading,total_rain_month=0, avg_rainfall_per_week; int i,j,k; //loops for(i=0;i<a.length;i++){ JOptionPane.showMessageDialog(null,"Week "+(i+1)); rainfall_per_week=Integer.parseInt(JOptionPane.showInputDialog("Enter the number of rainfall for week "+(i+1)+" ;")); for(j=0; j<rainfall_per_week; j++){ int tempreading=0; //temporary varible for(k=0; k<rainfall_per_week; k++){ reading=Integer.parseInt(JOptionPane.showInputDialog("Enter the reading for " + (j+1)+" :")); tempreading+= reading; //get the total amount of rainfall per week } a[i][j]=tempreading; //store the total amount of rainfall per week in the the array avg_rainfall_per_week=a[i][j]/4; //get the average total_rain_month+=a[i][j]; //get the total amount for the whole month } }
- 10-29-2010, 06:43 AM #5
What does "rainfall_per_week" keep track of exactly?
Sincerely, Joshua Green
Please REP if I help :)
- 10-29-2010, 07:16 AM #6
Better, but you could benefit from going throughSorry about the formating...
Code Conventions for the Java(TM) Programming Language: Contents
For posting on a forum, it's better to use two to four spaces, not tabs, per indent. and try to avoid unnecessary indents -- in your posted code the minimum indent is 2 tabs. That should be zero.
db
- 10-29-2010, 08:29 AM #7
Member
- Join Date
- Oct 2010
- Posts
- 6
- Rep Power
- 0
Things messd up in my application..ill re-write
Forget about my code now, i need to learn how create a table with number of rows, row number 1 with 5 coulmns, row2 with 10, row3 1 columns or just make it optional for the user to creat...
- 10-29-2010, 08:42 AM #8
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
You already defined your array as new int[4][];
That means you only specified the number of rows. All you need to do now is loop through, and instantiate a new one dimensional array at every row, that way you get your "ragged" array:
Hope this helps.Java Code:int[][] array = new int[10][]; for(int i = 0; i < array.length; i++) { int numColumns = getInputFromUser(); //your method goes here array[i] = new int[numColumns]; }Ever seen a dog chase its tail? Now that's an infinite loop.
- 10-30-2010, 01:00 AM #9
Member
- Join Date
- Oct 2010
- Posts
- 6
- Rep Power
- 0
ty m00nchile, i got the idea and solved the problem
Now I am trying to add some calculation but not getting the logic lol.Java Code:import javax.swing.JOptionPane; public class rainfall { public static void main(String args[]) { int array[][]=new int[4][]; int i,j,colSize; for(i=0; i<array.length;i ++) { colSize =Integer.parseInt(JOptionPane.showInputDialog("How many times didItRain in week"+(i+1)+":")); array[i]= new int[colSize]; } for(i=0; i<array.length; i++) { JOptionPane.showMessageDialog(null,"Week number "+(i+1)+" : "); for(j=0; j<array[i].length; j++) { array[i][j] =Integer.parseInt(JOptionPane.showInputDialog("Enter the readingAmount for"+(j+1)+":")); } } String display; for(i=0;i<array.length;i++) { display ="Reading for week "+(i+1)+" \n\n"; for(j=0; j<array[i].length; j++) { display+=array[i][j]+" "; } JOptionPane.showMessageDialog(null,display+" "); } }//end of main } //end of application
lets say the user created 2 columns in week 1, 3 in week 2, 1 in week 3 and 5 in week 4.
Now, I want the program to add 2 more columns for every week
one to get the total of the week, another for the average of the week
my table should look like this:
Table created by the user
W1 R1 R2
W2 R1 R2 R3
W3 R1
W4 R1 R2 R3 R4 R5
The brogram should create
W1 R1 R2 TW1 AvgW1
W2 R1 R2 R3 TW2 AvgW2
W3 R1 TW3 AvgW3
W4 R1 R2 R3 R4 R5 TW4 AvgW4
W=week, R=reading amount, TW=total for week, Avgw= average for week
and maybe i want to the program to add 1 row to calculate the total of the month.
Is that possible???
Similar Threads
-
convert byte array into char array
By kgkamaraj in forum New To JavaReplies: 4Last Post: 09-13-2011, 11:32 AM -
Variable of an object in an array compared to an element of another array?
By asmodean in forum New To JavaReplies: 23Last Post: 09-07-2010, 08:12 PM -
Trying to make an array list // inserting an element to middle of array
By javanew in forum New To JavaReplies: 2Last Post: 09-06-2010, 01:03 AM -
Array length and printing out uninitialized array.
By nicolek808 in forum New To JavaReplies: 4Last Post: 09-10-2009, 09:12 AM -
How to add an integer to a array element and the store that backinto an array.
By Hannguoi in forum New To JavaReplies: 1Last Post: 03-31-2009, 06:40 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks