FormatException是未处理的(来自数组的(int.parse)值)

时间:2017-04-29 00:01:01

标签: c# arrays combobox listbox

我正在编写一个程序,它从包含贷款信息的文本文件中的二维数组中获取数据。我终于想出了如何进入按钮的点击事件,现在我得到一个" FormatException未处理"尝试声明" currentValue"时出错通过解析数组的第三个值。我该如何解决这个问题?

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]));
            }
        }

    }

1 个答案:

答案 0 :(得分:1)

通过一些清理,你可以轻松解决问题。

步骤1:制作一个贷款类,其中包含有关贷款的所有数据的属性 - 贷款数量,贷款金额,利息等。

第2步:将string[,] loans = new string[4, 6];替换为List<Loan> MyLoans;

步骤3:使用贷款编号作为数据值并将金额或其他任何内容作为显示值,创建一个将组合框与MyLoans列表绑定的功能

第4步:在页面加载时,从文本文件中读取每一行,创建一个Loan对象并将其添加到MyLoans中。调用您在步骤3中创建的绑定功能。

步骤5:在按钮上单击忘记所选索引。使用所选项目的数据值。在MyLoans中找到此贷款并从中减去付款。

第6步:再次调用绑定函数,因为您已经更新了MyLoans - 它应该与新信息绑定。