Question: In my C# code I'm trying to add exception handling, so if the user types in something other than *, **, ***, ****, or *****

In my C# code I'm trying to add exception handling, so if the user types in something other than *, **, ***, ****, or ***** for a rating it will repeat until the user types in valid entry.

// This program read from a json file to retrive the year and title of some movies // prompting the user to rate them and saving the data back to the json file.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Text.Json; using System.Text.Json.Serialization;

namespace ConsoleAssignment4 { class Program { static void Main(string[] args) { multipleJson(); }

static void multipleJson() { int MoviesListCount = 0; string jsonDirectory = "JSON"; List theMovies = GetMoviesJSONFromFolder(jsonDirectory); if (theMovies.Count == 0) { Console.WriteLine("No valid JSON files found in the folder."); }

// go through the list one by one change the score if (theMovies != null && theMovies.Count > 0) { MoviesListCount = theMovies.Count;

for (int i = 0; i < MoviesListCount; i++) { Console.WriteLine(theMovies[i].title + " (" + theMovies[i].year + ")"); Console.WriteLine("Please provide rating for - " + theMovies[i].title + ": "); string userRating = Console.ReadLine(); theMovies[i].rating = userRating; } }

// print the movies with the rating Console.WriteLine("the rating for the movies are"); foreach (var aMovies in theMovies) { Console.WriteLine(aMovies.title + " (" + aMovies.year + ") - " + aMovies.rating); try { string jsonData = JsonSerializer.Serialize(aMovies); File.WriteAllText(aMovies.jsonFileName, jsonData); } catch (Exception ex) { Console.WriteLine("Could not write " + aMovies.id + " to file."); }

}

Console.ReadLine();

} static List GetMoviesJSONFromFolder(string jsonDirectory) { string[] jsonFiles; // checking if ther are no fles try { jsonFiles = Directory.GetFiles(jsonDirectory); } catch (Exception ex) { Console.WriteLine("Error: directory missing or no files present."); return new List { }; // return a blank list and kill the method } // initializing variables for the loop List MoviesList = new List { }; // placeholder Movies object Movies oneMovies = new Movies(); string jsonData = ""; // placeholder for the raw JSON data read from the file // copying all the JSON data into the list of objects // one JSON file per element in the list foreach (var jsonFile in jsonFiles) { // checking if the JSON file could not be converted to a Movies object try { jsonData = File.ReadAllText(jsonFile); oneMovies = JsonSerializer.Deserialize(jsonData); // converting the raw JSON string into a Movies object object MoviesList.Add(oneMovies); } catch (Exception ex) { Console.WriteLine("Error: invalid JSON file (" + jsonFile + ")"); }

} return MoviesList;

}

}

class Movies { public string id { get; set; } public string title { get; set; } public string year { get; set; } public string runtime { get; set; } public string genre { get; set; } public string rating { get; set; } public string jsonFileName { get; set; } } }

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!