파일을 읽고 매트릭스에 값을 입력하려고합니다. 입력 스팅이 올바른 형식이 아닌 오류가 발생했습니다. 또한 문제 해결 팁이 있습니다. "문자열을 DateTime으로 변환 할 때 각 변수를 DateTime 객체에 넣기 전에 날짜를 가져 오는 문자열을 구문 분석하십시오.이 코드는 아무데도 DateTime을 사용하지 않아 매우 이상합니다. 내가 바보 뭔가를 놓친 거지 확신 당신이 볼 필요가 코드의 일부를 잊어 버린 경우, 알려 주시기 바랍니다입력 형식이 올바르지 않습니다.
코드 (C#을) :.. 파일의
class Program
{
private static string line;
private static int[,] matrix;
private static int numOfCities;
private static int startCity;
private static int[] greedyPath;
private static int greedyRecursionCount;
private static int total;
private static int lowerBoundTotal;
private static string filename = "points.txt";
static void Main(string[] args)
{
readTxtFile();
getStartCity();
greedyStart();
totalTour();
lowerBound();
compareAll();
Console.ReadKey();
}
private static void readTxtFile()
{
numOfCities = 0;
TextReader tr = new StreamReader(filename);
numOfCities = int.Parse(tr.ReadLine().Trim());
line = "1";
int index = 0;
matrix = new int[numOfCities,numOfCities];
for (int i = 0; i < numOfCities; i++)
{
line = tr.ReadLine();
for (int j = 0; j < numOfCities; j++)
{
matrix[i, j] = int.Parse(line[index].ToString().Trim());
index = index + 2;
}//end inner for loop
index = 0;
}//end outer for loop
tr.Close();
}
}
내용 :
1000 //Number of points that are in the file
16 11 //XY-Points that are separated by 3 spaces
10 45
29 47
도움을 주시면 감사하겠습니다. 미리 감사드립니다.
이 코드는 어디에서나 "DateTime"에 대한 참조를 가지지 않으므로 이러한 구문 분석을 수행하지 않으므로 오류의 원인이 아닙니다. 예외가 _ 생한 곳을 확인하십시오. – Oded
아마도'int.Parse' 호출에서 오는 것입니다. – PhoenixReborn
정확한 메시지는 무엇입니까? 어떤 라인이 실패합니까? 입력 파일은 어떻게 생겼습니까? (나는 그것이 실행 시간에 실패한다고 생각한다.) – ThatBlairGuy