[英]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.