gpt4 book ai didi

c# - 输入字符串的格式不正确 C#

转载 作者:行者123 更新时间:2023-11-30 21:08:58 29 4
gpt4 key购买 nike

好吧,我对编程非常陌生,所以很可能这是一个非常简单的修复,但出于某种原因,我收到了一个Input string was not in a correct format error。当我尝试将文件中的字符串值解析为 double 时,我非常确定输入的字符串绝对是一个可以解析为 double 的数字。奇怪的是,我检查了数组,所有信息都完美地放入了数组。所以我真的不明白为什么会出现此错误。

这是我从中获取错误的方法的代码:

我在 LoadArray() 方法中收到错误 Input string was not in a correct formatannualInterestRate = Double.Parse(fields [0]);

这是我在输入文件中以相同格式获得的信息:

.015 20000 w8v6754r6 Jacobs,Michael
.045 60000 w4fg55566 Ray,Bill
.035 40000 w6gyhygg7 Nichols,Luke
.018 25000 w5g55g777 Hendricks,Gary

该类的其余代码只是以防万一,但实际上问题最有可能出在 LoadArray() 方法中。提前致谢!

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
}

最佳答案

检查您的文件并确保末尾没有空行。还要确保字段已正确填充。

我会在您遇到错误的那一行添加断点,然后逐行查看正在传递的数据。

希望对您有所帮助。

关于c# - 输入字符串的格式不正确 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9286319/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com