Lab: Exception handling to detect input String vs. Integer with Input File The given program opens a file named "NamesAndAges.txt" The given program reads a list of single-word first names and ages (ending with -1). The program outputs that list with the age incremented. The program fails and throws an exception if the 2nd input on a line is a String instead of an Int The program fails and throws an exception if the file can't be read Ex: If the contents of file NamesAndAges.txt are: Lee 18 Lua 21 Mary Beth 19 Stu 33 -1 then the output written to 'NamesAndAgesUpdated.txt": Lee 19 Lua 22 Mary 0 Stu 34
In C# I need help modifying my code for the attached lab.
//my code
using System;
using System.Collections.Generic;
using System.Linq;
class
{
static void Main()
{
Dictionary<string, int> numberNames = new Dictionary<string, int>();
string studentName = null;
string x = "-1";
int studentAge;
try
{
do
{
var userInput = Console.ReadLine();
if (userInput == "-1") break;
var data = userInput.Split(' ');
studentName = data[0];
studentAge = int.Parse(data[1]);
numberNames.Add(studentName, studentAge + 1);
} while (studentName != x);
}
catch (Exception e)
{
numberNames.Add(studentName, 0);
}
finally
{
string y = "-1";
string nameOne = null;
do
{
var inputLineOne = Console.ReadLine();
if (inputLineOne == "-1") break;
var data1 = inputLineOne.Split(' ');
nameOne = data1[0];
var ageOne = int.Parse(data1[1]);
numberNames.Add(nameOne, ageOne + 1);
} while (nameOne != y);
}
foreach (KeyValuePair<string, int> author in numberNames)
{
Console.WriteLine(author.Key + " " + author.Value);
}
}
}
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 1 images