잘 프로그래밍 할 수있어서 매우 쉽습니다. 이것은 매우 쉬운 수정이지만 "입력 문자열이 올바른 형식이 아닙니다."라는 메시지가 표시되는 이유입니다. 내가 문자열을 확실히 두 번 파싱 할 수있는 숫자라고 확신 할 때 파일에서 문자열 값을 파싱하려고 할 때 오류가 발생합니다. 이상한 점은 배열을 검사하고 모든 정보가 배열에 완벽하게 배치된다는 것입니다. 그래서 나는 왜 나는이 오류가 발생하는지 이해하지 못합니다. 여기 입력 문자열의 형식이 올바르지 않습니다. C#
내가에서 오류가있어 메서드에 대한 코드입니다 ". 입력 문자열이 올바른 형식이 아니었다"나는 오류가 발생"annualInterestRate = Double.Parse (fields [0]);"라는 줄의 LoadArray() 메서드에서
이
내가 같은 형식의 입력 파일에있는 정보입니다 :.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
}
'fields'에 무엇이 포함되어 있는지 보여줄 수 있습니까? – fuzz
그 줄에 중단 점을 넣고'fields' 내용을 검사 해 보셨습니까? –