I am writing a program that takes data from a 2d array in a text file containing loan information. I've finally figured out how to get into the click event for the button, and now I'm getting a "FormatException was unhandled" error when trying to declare "currentValue" by parsing the third value of the array. How can I fix this?
namespace
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string[,] loans = new string[4, 6];
int recordCount = 0;
private void Form1_Load(object sender, EventArgs e)
{
string currentLine;
string[] fields = new string[6];
int row = 0;
StreamReader loanReader = new StreamReader(@"C:\loans.txt");
while (loanReader.EndOfStream == false)
{
currentLine = loanReader.ReadLine();
fields = currentLine.Split(',');
loans[row, 0] = fields[0];
loans[row, 1] = fields[1];
loans[row, 2] = fields[2];
loans[row, 3] = fields[3];
loans[row, 4] = fields[4];
loans[row, 5] = fields[5];
row = row + 1;
}
recordCount = row;
loanReader.Close();
int nbrRows = 4;
txtPrincipal.Text = "0";
for (int i = 0; i < nbrRows; i++)
{
comboBox1.Items.Add(loans[i, 0]);
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
int row = comboBox1.SelectedIndex;
lstDisplay.Items.Clear();
string fmtStr = "{0,-15}{1,8}{2,30}";
string fmtStr2 = "{0,-15}{1,8:C}{2,40:C}";
lstDisplay.Items.Add(string.Format(fmtStr, "Loan #", "Original Balance", "Current Balance"));
lstDisplay.Items.Add(string.Format(fmtStr2, loans[row, 0], loans[row, 1], loans[row,2]));
}
private void btnRP_Click(object sender, EventArgs e)
{
int row = 0, currentLoan, interest, interestPmt, monthlyPmt, principalPmt, newBalance;
string selection = comboBox1.SelectedItem.ToString();
for (row = 0; row < recordCount; row++)
{
if (loans[row, 0] == selection)
{
currentLoan = int.Parse(loans[row, 2]);
interest = int.Parse(loans[row, 3]);
monthlyPmt = int.Parse(loans[row, 5]);
interestPmt = currentLoan * interest / 1200;
principalPmt = monthlyPmt - interestPmt;
newBalance = currentLoan - principalPmt;
loans[row, 2] = newBalance.ToString();
lstDisplay.Items.Clear();
string fmtStr = "{0,-15}{1,8}{2,30}";
string fmtStr2 = "{0,-15}{1,8:C}{2,40:C}";
lstDisplay.Items.Add(string.Format(fmtStr, "Loan #", "Original Balance", "Current Balance"));
lstDisplay.Items.Add(string.Format(fmtStr2, loans[row, 0], loans[row, 1], loans[row, 2]));
}
}
}
You're problem is a whole lot easier to fix with some clean up.
step 1: Make a Loan class that has properties for all the pieces of data about the loan - loan number, loan amount, interest, etc.
step 2: replace string[,] loans = new string[4, 6];
with List<Loan> MyLoans;
step 3: Make a function that binds the combobox with the list of MyLoans using the loan number as the datavalue and the amount or whatever as the displayvalue
step 4: On page load read each line from your text file creating a Loan object and adding it MyLoans. Call the binding function you created in step 3.
step 5: on the button click forget about the selected index. use the datavalue from the selected item. Locate this loan in MyLoans and subtract the payment from it.
step 6: call the bind function one more time now that you've updated MyLoans - it should bind with the new information.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.