So, today I was trying to make an app in which the user enters the coordinates of their car and the app calculates the easiest way to reach a gas station before the fuel tank runs out. But when I wanted to test if the app displays the data which is stored in a .txt
file, I get a System.FormatException
error which I cannot solve. I have used this method many many times before and I am not sure what is the problem here.
The error occurs at the line:
int xcoor = int.Parse(s.Substring(k + 1));
Code:
int n = 10;
string[] cities = new string[n];
int[] x = new int[n];
int[] y = new int[n];
System.IO.StreamReader sr = new System.IO.StreamReader(
@"..\..\CitiesGPS.txt", Encoding.GetEncoding(1250));
for (int i = 0; i < n; i++)
{
string s = sr.ReadLine();
int k = s.IndexOf(";");
string city = s.Substring(0, k);
cities[i] = city;
int xcoor = int.Parse(s.Substring(k + 1));
x[i] = xcoor;
int k2 = s.IndexOf(";");
int ycoor = int.Parse(s.Substring(k2 + 1));
y[i] = ycoor;
}
sr.Close();
Console.WriteLine("Data in the file:");
for (int i = 0; i < n; i++)
{
Console.WriteLine(cities[i] + ";" + x[i] + ";" + y[i]);
}
Data:
Nagyváros;150;30
Kisváros;250;300
TanyaBenzunkúttal;290;310
Szépváros;500;10
Alsóváros;250;0
Felsőváros;560;560
Középváros;300;300
FolyópartiVáros;380;400
HáromBenzinkutasváros;10;400
Nagyvárosbvezetőútja;380;230
Thanks in advance,
Blaise