Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello I need assistance with an error I\'m receiving. Text document for the savi

ID: 3586223 • Letter: H

Question

Hello I need assistance with an error I'm receiving.

Text document for the savings.txt

-----------------------------------------

Kumar:2144523950:10000:7.2
Peter:2154523000:1000:6
Juan :2154523000:1500:6

-------------------------------------------

Input string was not in a correct format.

-----------------------------------------------------------------------------------------------

Savings[] savingsAccounts = new Savings[3]; // declare an array of 3 Savings objects
//
Checkings[] checkingsAccounts = new Checkings[3]; // declare array of 3 Checkings objects
//
CreditCard[] cardAccounts = new CreditCard[3]; // declare array of 3 CreditCard objects
//
// must provide complete file path
String[] fnames = { "Savings.txt", "CreditCards.txt", "Checkings.txt" }; // create an array of all file names
//
String line; // this will store each line that is read
//
int i = 0; // this will be used to index the objects
//
// create a loop to iterate over each file name in the list above and read
foreach (String name in fnames)
{
//

//
StreamReader fread = new StreamReader(name); // create a stream reader object using a file and read data
//
line = fread.ReadLine();
//
String[] substrings;

//
// read till we reach the end of current file
while (line != null)
{
i = 0;
//
substrings = line.Split(':');
//
// verify the file type being currently read and store accordingly in one of the three arrays
if (name.Contains("Savings"))
{
//
savingsAccounts[i] = new Savings(substrings[0], Convert.ToInt32(substrings[1]), Convert.ToDouble(substrings[2]), Convert.ToInt32(substrings[3]));
//
i++;

}

Explanation / Answer

savingsAccounts[i] = new Savings(substrings[0], Convert.ToInt32(substrings[1]), Convert.ToDouble(substrings[2]), Convert.ToInt32(substrings[3]));

Take the file

Kumar:2144523950:10000:7.2
Peter:2154523000:1000:6
Juan :2154523000:1500:6

ur saying Convert.ToInt32(substrings[3]) observe in the first line of savings.txt last column 7.2 it is of real type n ur saying string to be converted to integer so it is throwing the error input string was not in correct format

so change Convert.ToInt32(substrings[3]) to Convert.ToDouble(substrings[3]) the problem will get solved

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote