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

I need help making the main program opens and reads the file and then prints the

ID: 3671468 • Letter: I

Question

I need help making the main program opens and reads the file and then prints the data to the screen.

import java.util.Scanner;

public class studentDataDetails
{
   //Variables
   public String name[][] = new String[10][10];
   private int numberData;
  
//Get Details of The Students
   public void getDetailsOfStudent(int n)
   {
   Scanner scanner = new Scanner(System.in);
   numberData = n;
  
   System.out.println("Enter "+numberData+" Student Details ");
  
   for(int i=0; i<numberData; i++)
   {
System.out.println("Enter Student " +(i+1)+ " Name, Idnumber & GPA: ");

for(int j=0;j<3;j++)
{
name[i][j] = scanner.nextLine();
}
   }
print(numberData);
   }
  
   //Display the Results
   public void print(int numberData)
   {
   System.out.println("Name"+" "+"Id number"+" "+"GPA");
  
   for(int i=0; i<numberData; i++)
   {
for(int j=0;j<3;j++)
{
System.out.print(name[i][j]+" ");
}
System.out.println();
   }
   }
  
}
--------------------

import java.io.*;
import java.util.Scanner;

public class mainStudentClass
{
public static void main(String args[])throws IOException
   {
  
   //local variables
   int n = 6;
  
   Scanner scanner = new Scanner(System.in);

//***************************************************************************************************//
  
   //Enter 6 student information
   studentDataDetails details = new studentDataDetails();
   details.getDetailsOfStudent(n);
  
   //Create a file for student data
   PrintStream output = new PrintStream(new File("studentDataDetails.txt"));
  
   output.println("Name"+" "+"Id number"+" "+"GPA");
  
   output.println("************************************");
for(int i=0;i<n;i++)
{
for(int j=0;j<3;j++)
{
output.print(details.name[i][j]+" ");
}
output.println();
   output.println("**************************************");
}
output.close();
  
   }
}

Write two programs. Submit both programs along with the data file the first program creates! Program 1: The first program will create a new file and place the following information about 6 students in the file: 1. Name 2. ID number 3. GPA Each of the above pieces of information is a field. One record contains all three fields - so there are three fields for each record. Each record represents one student. Have the program place 6 records in the new file. Program 2: The second program will open the file created by Program 1, read each record, and display each record on a separate line as follows: Name, ID number, GPA Please separate each field within the record with a comma and a space when you print it out. Don't forget the space! It's how we read things!

Explanation / Answer

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

public class studentDataDetails {
   // Variables
   public String name[][] = new String[10][10];
   private int numberData;

   public static void main(String[] args) {
       studentDataDetails dataDetails = new studentDataDetails();
       dataDetails.getDetailsOfStudent(6);

   }

   // Get Details of The Students
   public void getDetailsOfStudent(int n) {
       Scanner scanner = new Scanner(System.in);
       numberData = n;

       System.out.println("Enter " + numberData + " Student Details ");

       for (int i = 0; i < numberData; i++) {
           System.out.println("Enter Student " + (i + 1)
                   + " Name, Idnumber & GPA: ");

           for (int j = 0; j < 3; j++) {
               name[i][j] = scanner.nextLine();
           }
       }
       print(numberData);
       // writeData(numberData);
       scanner.close();
   }

   /*   *//**
   * writing data to file
   *
   * @param numberData
   */
   /*
   * public void writeData(int numberData) {
   *
   * try { File file = new File("studentDataDetails.txt"); // if file doesnt
   * exists, then create it if (!file.exists()) { file.createNewFile(); }
   *
   * FileWriter fw = new FileWriter(file.getAbsoluteFile()); BufferedWriter bw
   * = new BufferedWriter(fw); for (int i = 0; i < numberData; i++) { String
   * studentRecord = ""; for (int j = 0; j < 3; j++) {
   *
   * studentRecord += name[i][j] + " "; } bw.write(studentRecord+" ");
   * System.out.println(); }
   *
   * bw.close(); } catch (Exception e) { // TODO: handle exception } }
   */

   // Display the Results
   public void print(int numberData) {
       System.out.println("Name" + " " + "Id number" + " " + "GPA");

       for (int i = 0; i < numberData; i++) {
           for (int j = 0; j < 3; j++) {
               System.out.print(name[i][j] + " ");
           }
           System.out.println();
       }
   }

}

import java.io.*;
import java.util.Scanner;

public class mainStudentClass {
   public static void main(String args[]) throws IOException {

       // local variables
       int n = 6;

       File file = new File("studentDataDetails.txt");

       // **********************************************************************************
       // *****************

       // Enter 6 student information
       studentDataDetails details = new studentDataDetails();
       details.getDetailsOfStudent(n);

       // Create a file for student data
       PrintStream output = new PrintStream(file);

       output.println("Name" + " " + "Id number" + " " + "GPA");

       output.println("************************************");
       for (int i = 0; i < n; i++) {
           for (int j = 0; j < 3; j++) {
               output.print(details.name[i][j] + " ");
           }
           output.println();
           output.println("**************************************");
       }
       output.close();
       // reading and printing from files
       Scanner scanner = new Scanner(file);
       System.out.println("Printing From file");
       System.out.println(scanner.nextLine());

       while (scanner.hasNext()) {
           scanner.nextLine();
           if (scanner.hasNext()) {
               String line = scanner.nextLine();
               String lineArray[] = line.split("       ");
               System.out.println(lineArray[0] + "," + lineArray[1] + ","
                       + lineArray[2]);
           }

       }

       scanner.close();

   }
}

studentDataDetails.txt:
Name   Id number       GPA
************************************
Srinivas       1       98      
**************************************
Neehaal       68       96      
**************************************
Akshaya       3       99      
**************************************
Madhu       99       99      
**************************************
Ajay       5       99      
**************************************
Vijay       6       99      
**************************************

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