Reputation: 25
Alright, I'm extremely new to programming so odds are this is a really easy fix but for some reason, I'm receiving an Input string was not in a correct format error
. When I try to parse a string value from a file to double when I'm pretty sure the string being input is definitely a number that can be parsed to double. What's strange is that I checked the array and all the info gets placed into the array perfectly. So I really don't understand why I'm getting this error.
Here's the code for the method that I'm getting the error from:
I receive the error Input string was not in a correct format
in the method LoadArray()
at the line that says annualInterestRate = Double.Parse(fields[0]);
.
This is the info I have in the input file in the same formatting:
.015 20000 w8v6754r6 Jacobs,Michael
.045 60000 w4fg55566 Ray,Bill
.035 40000 w6gyhygg7 Nichols,Luke
.018 25000 w5g55g777 Hendricks,Gary
The rest of the code from that class is just there in case, but really the problem is most likely in the LoadArray()
method. Thanks in advance!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace SavingsAccount
{
class Driver
{
public static void LoadArray(SavingsAccount [ ] array)
{
string accountHolder = " ";
string accountNumber = " ";
double annualInterestRate = 0.0;
double savingsBalance = 0.0;
int i = 0;
StreamReader inFile = new StreamReader("accountdata.txt");
SavingsAccount account1 = new SavingsAccount();
//set to default constructor
for (int j = 0; j < array.Length; j++)
array[j] = new SavingsAccount();
while (!inFile.EndOfStream)
{
//input file
string[] fields = inFile.ReadLine().Trim().Split(' ');
annualInterestRate = Double.Parse(fields[0]);
savingsBalance = Double.Parse(fields[1]);
accountNumber = fields[2];
accountHolder = fields[3];
//annualInterestRate = Double.Parse(inFile.ReadLine());
//savingsBalance = Double.Parse(inFile.ReadLine());
//accountNumber = inFile.ReadLine();
//accountHolder = inFile.ReadLine();
//set info from input file
account1.setAccountNumber(accountNumber);
account1.setAccountHolder(accountHolder);
account1.ModifyInterestRate(annualInterestRate);
account1.ModifySavingsBalance(savingsBalance);
array[i] = new SavingsAccount(annualInterestRate, savingsBalance, accountNumber, accountHolder);
i++;
}//end while
inFile.Close();
}//end method
public static void SavingsReport( SavingsAccount [ ] array)
{
SavingsAccount account1 = new SavingsAccount();
Console.WriteLine("------------------------Savings Report-----------------------------");
Console.WriteLine("Interest Rate Savings Balance Account Number Account Holder");
Console.WriteLine("-------------------------------------------------------------------");
for (int i = 0; i < array.Length; i++)
{
Console.WriteLine(account1.ToString());
}
Console.WriteLine("-------------------------------------------------------------------");
Console.WriteLine();
//use array as parameter
}
public static void StoreAccounts( SavingsAccount [ ] array)
{
SavingsAccount account1 = new SavingsAccount();
StreamWriter outFile = new StreamWriter(@"C:\Users\Juan D Sanchez\Desktop\accountholderdata.txt");
//use array as parameter
outFile.WriteLine("------------------------Savings Report-----------------------------");
outFile.WriteLine("Interest Rate Savings Balance Account Number Account Holder");
outFile.WriteLine("-------------------------------------------------------------------");
for (int i = 0; i < array.Length; i++)
{
outFile.WriteLine(account1.ToString());
}
outFile.WriteLine("-------------------------------------------------------------------");
outFile.WriteLine();
}
static void Main(string[] args)
{
SavingsAccount[] Accounts = new SavingsAccount[100];
LoadArray(Accounts);
SavingsReport(Accounts);
StoreAccounts(Accounts);
//for (int i = 0; i < Accounts.Length; i++)
//{
//}
//exits the program after the user enters any key
Console.WriteLine("Press any key to close.....");
Console.ReadKey();
}//end main
}//end class
}
Upvotes: 2
Views: 6921
Reputation: 2075
Check your file and make sure there's no empty line at the end. Also make sure that fields are veing populated correctly.
I would add breakpoint at the line you are getting the error and go through line by line to find out what data is being passed.
Hope it helps.
Upvotes: 2
Reputation: 4028
It looks like a culture problem. Try double.Parse(fields[0], CultureInfo.InvariantCulture);
Upvotes: 2