Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

CONVERT TO C++ (I can handle the input/output files) | import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.FileWriter; import java.io.PrintWriter; import java.util.Scanner; import java.util.Formatter; public

CONVERT TO C++ (I can handle the input/output files)

|

import java.io.File;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.FileWriter;

import java.io.PrintWriter;

import java.util.Scanner;

import java.util.Formatter;

public class StudentScoresFileIO

{

public static void main(String[] args)

{

//declare and initialize variables

int totalScore, scoreCount;

int studentId1, studentId2;

double score, avgScore;

String studentLastName, studentFirstName;

String outputStr = "";

String formatStr = "%-8s%-15s%-15s%6.2f ";

Scanner namesInput = null, scoresInput = null;

File inStudentsFile = new File("studentNames.txt");//File objects

File inScoresFile = new File("studentScores.txt");

File outFile = new File("averages.txt");

FileWriter aFileWriter = null;

PrintWriter aPrintWriter = null;

try

{

namesInput = new Scanner(inStudentsFile);//Scanner objects

scoresInput = new Scanner(inScoresFile);

aFileWriter = new FileWriter(outFile);//create pipeline

aPrintWriter = new PrintWriter(aFileWriter);//create valve

while (namesInput.hasNext())

{

totalScore = 0;//initialized for new student

scoreCount = 0;

avgScore = 0;

//input

studentId1 = namesInput.nextInt();

studentLastName = namesInput.next();

studentFirstName = namesInput.next();

while (scoresInput.hasNext())

{

//processing

studentId2 = scoresInput.nextInt();//get ID

score = scoresInput.nextDouble();//get score

if (studentId2 == studentId1)//match student ID'sstem

{

totalScore += score;

scoreCount++;

}//end if

}//end inner loop

avgScore = (double)totalScore / scoreCount;

//add data to output string

outputStr += String.format

(formatStr, studentId1, studentLastName, studentFirstName, avgScore);

//reset scores scanner for new student

scoresInput = new Scanner(inScoresFile);

}//end outer loop

//output

System.out.println(outputStr);//print data to window

aPrintWriter.print(outputStr);//print data to file

}//end try

catch (FileNotFoundException e)

{

System.err.println(e.getMessage());

System.exit(1);

}

catch (IOException e)

{

System.err.println(e.getMessage());

System.exit(1);

}

finally

{

namesInput.close();

scoresInput.close();

aPrintWriter.close();

}

}//end main

}//end class

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Sams Teach Yourself Beginning Databases In 24 Hours

Authors: Ryan Stephens, Ron Plew

1st Edition

067232492X, 978-0672324925

More Books

Students also viewed these Databases questions