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

This isa Java program Create file, file 1, with the following values: 3 4 35 20

ID: 3793131 • Letter: T

Question

This isa Java program

Create file, file 1, with the following values: 3 4 35 20 -43 17 -10 6 7 -2 13 1 2 3 Write a program in Java to do the following: Open "file 1" and read the first two values, rand c, which are supposed to indicate the dimensions of the two-dimensional array whose values are given in the file after the values of r and c. Create a two-dimensional array of size r times c. Populate the two-dimensional array created in the previous step by reading values from file 1. -Write a function to print the two-dimensional array. Pass the array to the function using PBR method, and pass the values of r and c using PBV method.

Explanation / Answer

ArrayFromFile.java

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


public class ArrayFromFile {

  
   public static void main(String[] args) throws FileNotFoundException {
       File file = new File("D:\file1.txt");
       Scanner scan = new Scanner(file);
       int r = scan.nextInt();
       int c = scan.nextInt();
       int a[][] = new int[r][c];
       readValues(a, scan);
       printArray(a);
   }
   public static void readValues(int a[][], Scanner scan){
       for(int i=0; i<a.length; i++){
           for(int j=0; j<a[i].length; j++){
               a[i][j] = scan.nextInt();
           }
       }
   }
   public static void printArray(int a[][]){
       for(int i=0; i<a.length; i++){
           for(int j=0; j<a[i].length; j++){
               System.out.print(a[i][j]+" ");
           }
           System.out.println();
       }
   }

}

Output:

35 20 -43 17
-10 6 7 -2
13 1 2 3

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote