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);
}
}
}
![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](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F8994056b-9424-4df9-b1dd-94767d8f70ea%2F008f3fa8-8af6-4784-b863-4c93f5de68f3%2Fx8sm81a_processed.jpeg&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 1 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)