Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

import java.io.File; import java.io.FileNotFoundException; import java.util.Scan

ID: 3568696 • Letter: I

Question

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Lab12 {

public static void main(String[] args) ??? ??? {
  
  /*
   * This lab exercise is about reading a table of numbers from a text file and storing it in a 2-d array
   * Then you calculate the column-wise sum of table of numbers and then print it out onto the console.
   */
  
  // Declare Scanner class for reading from the file
  Scanner ??? = new Scanner(??? ???(???));
  
  // Define the 2-d array with dimensions 10x5
  ??? ??? = ??? ???[???][???];
  
  //Read from the file into the above 2-d array
  for(int i=0 ; i<??? ; ???){
   for(int j=0 ; j<??? ; ???){
    // Read the number from file and store in the 2-d array
    
   }
  }
  
  //Finding the column-wise sum of the 2-d array
  // Create a 1-d array of length 5 and store the corresponding column sums in the 1-d array
  int[] sum = ??? ???[???];
  for(??? ; ??? ; ???){
   for(??? ; ??? ; ???){
    sum[???] += ???;
   }
  }
  
  // Print the sum
  for(??? ; ??? ; ???){
   //Print the sum
  }
}
}

Explanation / Answer

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Lab12 {

public static void main(String[] args) throws FileNotFoundException {

/*
   * This lab exercise is about reading a table of numbers from a text file and storing it in a 2-d array
   * Then you calculate the column-wise sum of table of numbers and then print it out onto the console.
   */

// Declare Scanner class for reading from the file
Scanner scanner = new Scanner(new File("textfile.txt"));

// Define the 2-d array with dimensions 10x5
int array[][] = new int[10][5];

//Read from the file into the above 2-d array
for(int i=0 ; i<array.length ; i++){
   for(int j=0 ; j<array[i].length ; j++)
{
    // Read the number from file and store in the 2-d array
  
   }
}

//Finding the column-wise sum of the 2-d array
// Create a 1-d array of length 5 and store the corresponding column sums in the 1-d array
int[] sum = new int[5];
for(int i=0 ; i<array.length ; i++){
       for(int j=0 ; j<array[i].length ; j++)
{
     sum[j] += array[i][j];
   }
}

// Print the sum
for(int i=0 ; i<5 ; i++){
   //Print the sum
}
}
}