Results 1 to 9 of 9

Thread: 2D array

  1. #1
    klexe is offline Member
    Join Date
    Oct 2010
    Posts
    6
    Rep Power
    0

    Default 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 :)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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!

  3. #3
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default

    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 :)

  4. #4
    klexe is offline Member
    Join Date
    Oct 2010
    Posts
    6
    Rep Power
    0

    Default

    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
    				}
    		}

  5. #5
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default

    What does "rainfall_per_week" keep track of exactly?
    Sincerely, Joshua Green
    Please REP if I help :)

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,240
    Rep Power
    19

    Default

    Sorry about the formating...
    Better, but you could benefit from going through
    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

  7. #7
    klexe is offline Member
    Join Date
    Oct 2010
    Posts
    6
    Rep Power
    0

    Default

    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...

  8. #8
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    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:
    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];
    }
    Hope this helps.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  9. #9
    klexe is offline Member
    Join Date
    Oct 2010
    Posts
    6
    Rep Power
    0

    Default

    ty m00nchile, i got the idea and solved the problem
    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
    Now I am trying to add some calculation but not getting the logic lol.

    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

  1. convert byte array into char array
    By kgkamaraj in forum New To Java
    Replies: 4
    Last Post: 09-13-2011, 11:32 AM
  2. Replies: 23
    Last Post: 09-07-2010, 08:12 PM
  3. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  4. Array length and printing out uninitialized array.
    By nicolek808 in forum New To Java
    Replies: 4
    Last Post: 09-10-2009, 09:12 AM
  5. Replies: 1
    Last Post: 03-31-2009, 06:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •