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();
}
}
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
**************************************
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.