C# 输入字符串的格式不正确#
好的,我对编程非常陌生,所以很可能这是一个非常简单的修复方法,但由于某种原因,我收到了一个C# 输入字符串的格式不正确#,c#,C#,好的,我对编程非常陌生,所以很可能这是一个非常简单的修复方法,但由于某种原因,我收到了一个输入字符串格式不正确的错误。当我尝试将文件中的字符串值解析为double时,我非常确定输入的字符串肯定是一个可以解析为double的数字。奇怪的是,我检查了数组,所有信息都完美地放入了数组中。所以我真的不明白为什么我会犯这个错误 下面是我从中获取错误的方法的代码: 我收到错误方法LoadArray()中的输入字符串格式不正确,该行显示annualInterestate=Double.Parse(字段[0])
输入字符串格式不正确的错误。当我尝试将文件中的字符串值解析为double时,我非常确定输入的字符串肯定是一个可以解析为double的数字。奇怪的是,我检查了数组,所有信息都完美地放入了数组中。所以我真的不明白为什么我会犯这个错误
下面是我从中获取错误的方法的代码:
我收到错误方法LoadArray()
中的输入字符串格式不正确,该行显示annualInterestate=Double.Parse(字段[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
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.IO;
名称空间SavingsAccount
{
类驱动程序
{
公共静态void加载数组(SavingsAccount[]数组)
{
字符串accountHolder=“”;
字符串accountNumber=“”;
双年度利率=0.0;
双储蓄天平=0.0;
int i=0;
StreamReader infle=新的StreamReader(“accountdata.txt”);
SavingsAccount account1=新的SavingsAccount();
//设置为默认构造函数
for(int j=0;j
这看起来像是文化问题。尝试double.Parse(字段[0],CultureInfo.InvariantCulture)代码>这看起来像是一个文化问题。尝试double.Parse(字段[0],CultureInfo.InvariantCulture)代码>检查文件,确保结尾没有空行。还要确保字段已正确填充
我会在您得到错误的那一行添加断点,并逐行检查以了解传递的是什么数据
希望有帮助。检查您的文件,确保结尾没有空行。还要确保字段已正确填充
我会在您得到错误的那一行添加断点,并逐行检查以了解传递的是什么数据
希望能有所帮助。在进行拆分之前,您能否向我们展示字段
包含的内容?您是否尝试在该行上放置断点并检查字段
的内容?您能否向我们展示字段
的内容