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

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter14: Exception Handling
Section: Chapter Questions
Problem 1TF
icon
Related questions
Question

In C# I need help modifying my code for the attached lab.

//my code

using System;
using System.Collections.Generic;
using System.Linq;
class program
{
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
Transcribed Image Text: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
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 1 images

Blurred answer
Knowledge Booster
Concept of memory addresses in pointers
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT