Question: Convert to C# 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[]

Convert to C#

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!