繁体   English   中英

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

[英]FormatException was Unhandled ((int.parse) value from an array)

我正在编写一个程序,该程序从包含贷款信息的文本文件中的2d数组中获取数据。 我终于弄清楚了如何进入按钮的click事件,现在尝试通过解析数组的第三个值来声明“ currentValue”时,出现“ FormatException未处理”错误。 我怎样才能解决这个问题?

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:制作一个Loan类,该类具有有关贷款的所有数据的属性-贷款数量,贷款金额,利息等。

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

第3步:制作一个函数,使用借贷号作为数据值和金额或任何值作为显示值,将组合框与MyLoans列表绑定

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

步骤5:在按钮上单击“忘记所选索引”。 使用所选项目中的数据值。 在“我的贷款”中找到这笔贷款,并从中减去还款额。

步骤6:在您更新MyLoans之后,再次调用bind函数-它应该与新信息绑定。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM