This a portion of my code with 2 sets of 2d array

public class Form2
{
public static void main(String[] args){

int[][] dayForm = { {25,15,28,17,19},
                    {31,21},
                    {23,24,19,18},
                    {15,19,22},
                    {14,17,13,18},
                    {8,12}};

int[][] boardForm = { {10,18,17,17,15},
                      {16,9},
                      {13,19,15,25},
                      {11,10,8},
                      {14,17,19,13},
                      {11,4}};


final int SIZE = 4;
int[] totalday = new int[SIZE];

showaverage(dayForm);





}}

public static void showaverage(int[][] arr)
{int total,average;
int form = 1;

System.out.println("Day Students :");
 for(int row = 0;row <dayForm.length;row++)
{  total=0;
   average=0;  
  
for(int col = 0;col < dayForm[row].length;col++)
   {total +=dayForm[row][col];} 
   average = total / dayForm[row].length;
   System.out.println("Form "+form+" average students is "+average);
   form++;

}
}

It seems that i encounter a problem in passing the 2d array to the method.There a so many errors of "class ,interface or enum expected",anyway to deal with it?

Recommended Answers

All 3 Replies

This a portion of my code with 2 sets of 2d array

public class Form2
{
public static void main(String[] args){

int[][] dayForm = { {25,15,28,17,19},
                    {31,21},
                    {23,24,19,18},
                    {15,19,22},
                    {14,17,13,18},
                    {8,12}};

int[][] boardForm = { {10,18,17,17,15},
                      {16,9},
                      {13,19,15,25},
                      {11,10,8},
                      {14,17,19,13},
                      {11,4}};


final int SIZE = 4;
int[] totalday = new int[SIZE];

showaverage(dayForm);





}}

public static void showaverage(int[][] arr)
{int total,average;
int form = 1;

System.out.println("Day Students :");
 for(int row = 0;row <dayForm.length;row++)
{  total=0;
   average=0;  
  
for(int col = 0;col < dayForm[row].length;col++)
   {total +=dayForm[row][col];} 
   average = total / dayForm[row].length;
   System.out.println("Form "+form+" average students is "+average);
   form++;

}
}

It seems that i encounter a problem in passing the 2d array to the method.There a so many errors of "class ,interface or enum expected",anyway to deal with it?

Here is the code that may work your cause..
The problem was that the parameter for show average is arr and you were using dayform insteadly......


import java.io.*;

public class Form2
{
public static void main(String[] args){

int[][] dayForm = { {25,15,28,17,19},
{31,21},
{23,24,19,18},
{15,19,22},
{14,17,13,18},
{8,12}};

int[][] boardForm = { {10,18,17,17,15},
{16,9},
{13,19,15,25},
{11,10,8},
{14,17,19,13},
{11,4}};


final int SIZE = 4;
int[] totalday = new int;

showaverage(dayForm);

}

public static void showaverage(int[][] arr)
{int total,average;
int form = 1;

System.out.println("Day Students :");
for(int row = 0;row <arr.length;row++)
{ total=0;
average=0;

for(int col = 0;col < arr[row].length;col++)
{total +=arr[row][col];}
average = total / arr[row].length;
System.out.println("Form "+form+" average students is "+average);
form++;

}
}
}

While writing and editing this program you have not been careful with brackets. Your class immediately close after main method and showaverage method is left out of the class. So either you move closing bracket at the end of showaverage or you put this method in new class (intern class).
dayForm is local variable so you cannot call it outside of the main method.
Here are correction to the program

public class Form2
{
	public static void main(String[] args){
	
		int[][] dayForm = { {25,15,28,17,19},
		                    {31,21},
		                    {23,24,19,18},
		                    {15,19,22},
		                    {14,17,13,18},
		                    {8,12}};
		
		int[][] boardForm = { {10,18,17,17,15},
		                      {16,9},
		                      {13,19,15,25},
		                      {11,10,8},
		                      {14,17,19,13},
		                      {11,4}};
		
		
		final int SIZE = 4;
		int[] totalday = new int[SIZE];
		
		showaverage(dayForm);	
	}

	public static void showaverage(int[][] arr)
	{
		int total,average;
		int form = 1;
		
		System.out.println("Day Students :");
		 for(int row = 0;row <arr.length;row++)
		{  
			total=0;
			average=0;
			
			for(int col = 0;col < arr[row].length;col++){
				total +=arr[row][col];
			}
			average = total / arr[row].length;
			System.out.println("Form "+form+" average students is "+average);
			form++;
		}
	}
}

Thanks to u guys ,the above mentioned problem was overcome.Now i faced another problem:

import java.util.Arrays;

public class Form2
{

public static void main(String[] args){

int[][] dayForm = { {25,15,28,17,19},
                    {31,21},
                    {23,24,19,18},
                    {15,19,22},
                    {14,17,13,18},
                    {8,12}};

int[][] boardForm = { {10,18,17,17,15},
                      {16,9},
                      {13,19,15,25},
                      {11,10,8},
                      {14,17,19,13},
                      {11,4}};


int SIZE = 6;
int[] totalday = new int[SIZE] ;
int[] totalboard = new int[SIZE];


System.out.println("\nDay Students :");
showaverage(dayForm,totalday);
System.out.println("\nBoarding Students :");
showaverage(boardForm,totalboard);
System.out.println("\nTotal payment of each form of Day Students:");
paymentDay(totalday);
System.out.println("\nTotal payment of each form of Boarding Students:");
paymentBoard(totalboard);








}

public static void showaverage(int[][] arr,int[] tot)
{int total,average;
int form = 1;


 for(int row = 0;row <arr.length;row++)
{  total=0;
   average=0;  
  
for(int col = 0;col < arr[row].length;col++)
   {total +=arr[row][col];} 
   tot[row]=total;
   average = total / arr[row].length;
   System.out.println("Form "+form+" average students is "+average);
   form++;

}
}

public static void paymentDay(int[] tot)
{
  int form = 1;

 for(int col=0;col<6;col++)
{ 
  if(col >=0 && col <3)
    { tot[col] = tot[col]*1500;
      System.out.printf("Form "+form+" : RM%.2f\n",tot[col]);
     }
   if(col == 3 || col == 4 )
    { tot[col] = tot[col]*2500;
      System.out.println("Form "+form+" : RM"+tot[col]);
     }
     if(col == 5 )
    { tot[col] = tot[col]*2700;
      System.out.println("Form "+form+" : RM"+tot[col]);
     }
  
  form++;
}



}


public static void paymentBoard(int[] tot)
{
  int form = 1;

 for(int col=0;col<6;col++)
{ 
  if(col >=0 && col <3)
    { 
      System.out.println("Form "+form+" : RM"+(tot[col]*3000));
     }
  
  if(col >=3 && col <5)
    {
     System.out.println("Form "+form+" : RM"+(tot[col]*4000));
    }
  if(col ==5)
       
      System.out.println("Form "+form+" : RM"+(tot[col]*4500));

   form++;
}

}




}

The lines of System.out.printf("Form "+form+" : RM%.2f\n",tot[col]);
cause an error .How can i output the value so that it have 2 decimal point ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.