Question: Write an application that retrieves both string data and numbers from a text file. Test your solution by retrieving names of students and three scores
Write an application that retrieves both string data and numbers from a text file. Test your solution by retrieving names of students and three scores per linen from a text file. Process the values by calculating the average scores per student. Write the name and average to a different text file. Display on the console screen what is being written to the new file. Test your application with a minimum of eight records in the original file. Hint: You might consider adding delimiters between the data values in the original text file to simplify retrieving and processing the data. Include appropriate exception-handling techniques in your solution. When the application closes, locate the text file and verify its contents.
My program has has the following problems; Calculation for average is wrong probably something simple. Exception handling show error. Not close properly.
This is the class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestScores
{
class Scores
{
private string firstName;
private string lastName;
private double firstScore;
private double secondScore;
private double thirdScore;
public Scores()
{
}
//constructors
public Scores(string fName, string lName, double scoreOne, double scoreTwo, double scoreThree)
{
firstName = fName;
lastName = lName;
firstScore = scoreOne;
secondScore = scoreTwo;
thirdScore = scoreThree;
}
//Properties
public string fName { get; set; }
public string lName { get; set; }
public double scoreOne { get; set; }
public double scoreTwo { get; set; }
public double scoreThree { get; set; }
}// end of class
}// end of namespace
The program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO; //added for file access
using static System.Console;
namespace TestScores
{
class Grade
{
class Program
{
static void Main(string[] args)
{
{
bool isGood = true;
try
{
string testData = @"C:\Documents\Visual Studio 2017\TestScores\TestScores\scores.txt"; //variable assigned to path
// if the directory doesn't exist
if (!File.Exists(testData))
{
WriteLine("{ 0} not found");
}
{
//Creating a List.
List
List
}
}
catch (System.IO.IOException exc)
{
isGood = false;
}
finally
{
if (!isGood)
{
Scores = null;
}
}
return isGood;
}
foreach (var line in lines)
{
string[] entries = line.Split(','); //splitting items
Scores newScores = new Scores();
newScores.fName = entries[0];
newScores.lName = entries[1];
newScores.scoreOne = double.Parse(entries[2]);
newScores.scoreTwo = double.Parse(entries[3]);
newScores.scoreThree = double.Parse(entries[4]);
grades.Add(newScores);
}
WriteLine("Read from text file");
foreach (var scores in grades)
{
WriteLine($"{scores.fName} {scores.lName}: {scores.scoreOne} {scores.scoreTwo} {scores.scoreThree}");
}
grades.Add(new Scores { fName = "Christine", lName = "Jones", scoreOne = 98, scoreTwo = 100, scoreThree = 88 });
WriteLine("Writing to text file");
List
foreach (var scores in grades)
{
output.Add($"{ scores.fName}, {scores.lName}, {scores.scoreOne}, {scores.scoreTwo}, {scores.scoreThree}");
}
File.WriteAllLines(testData, output);
WriteLine("All entries written");
//Calculate the average
foreach (var scores in grades)
{
scores.fName + scores.lName + '|' + ((scores.scoreOne + scores.scoreTwo + scores.scoreThree) / 3.0).ToString("N3");
}
ReadLine();
try
{
testData.Close();
}
catch
{
WriteLine("file did not close properly");
}
}
}
}
}
The file name scores.txt
Richie Rich 100,80, 88
James Brown 85, 98, 92
Lisa Unsure 75, 85,87
David Devine 90,93,98
Edward Pruitt 88,83,85
Sharon Unstudy 87, 80, 88
Chirs Unreliable 90,97,92
Dewayne Wantmore 72,75,75
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
