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

Problem 5 (name this Lab9_Problem5) 1. Create your own Java program that creates

ID: 3688162 • Letter: P

Question

Problem 5 (name this Lab9_Problem5) 1. Create your own Java program that creates a token-based file of composed of homogenous data of numeric data (int or double—your choice). Use only a single main() method to do everything. 2. The file can have as many rows as the user wants, up to 100, and as many columns as the user wants, up to 8. Get this information in main() and then pass it on to a method named fiCreateFile. 3. Populate it with random integers or doubles based on the data type you choose. 4. As before, have main() receive the method's return value and display success or failure: File processing complete or Unable to create output file 5. Verify that the file exists and contains the proper output by searching for it and opening it in Notepad.

Explanation / Answer

Please find the required solution:

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
import java.util.Scanner;

public class CreateFileClass {

   // test method to read number of rows and columns
   public static void main(String[] args) throws IOException {
       // instantiate scanner class
       Scanner keyboard = new Scanner(System.in);
       System.out.print("Enter number of rows:");
       int rows = keyboard.nextInt();
       System.out.print("Enter number of columns:");
       int columns = keyboard.nextInt();

       // invoke method to write to file
       fiCreateFile(rows, columns);
       keyboard.close();
   }

   // please find the required method
   public static void fiCreateFile(int rows, int columns) throws IOException {
       Random rdm = new Random();

       // Prepare and write to file
       File file = new File("outputfile.txt");
       FileWriter fwriter = new FileWriter(file);
       BufferedWriter bwriter = new BufferedWriter(fwriter);

       // write to file line by name
       for (int i = 0; i < rows; i++) {
           StringBuffer buffer = new StringBuffer();
           for (int j = 0; j < columns - 1; j++) {
               buffer.append(rdm.nextInt() + "   ");
           }
           buffer.append(rdm.nextInt());
           bwriter.write(buffer.toString());
           bwriter.newLine();
       }
       bwriter.close();
   }
}