I have an float array which is a[][] and a temporary reading array which is b[]. I have data like this:
1 1 6 4 12 5 5 3 4 1 67 3 2 1 2 1 0 0 1 0 0 1 0 0 1
2 2 48 2 60 1 3 2 2 1 22 3 1 1 1 1 0 0 1 0 0 1 0 0 1
1 4 12 4 21 1 4 3 3 1 49 3 1 2 1 1 0 0 1 0 0 1 0 1 0
1 1 42 2 79 1 4 3 4 2 45 3 1 2 1 1 0 0 0 0 0 0 0 0 1
2 1 24 3 49 1 3 3 4 4 53 3 2 2 1 1 1 0 1 0 0 0 0 0 1
1 4 36 2 91 5 3 3 4 4 35 3 1 2 2 1 0 0 1 0 0 0 0 1 0
1 4 24 2 28 3 5 3 4 2 53 3 1 1 1 1 0 0 1 0 0 1 0 0 1
1 2 36 2 69 1 3 3 2 3 35 3 1 1 2 1 0 1 1 0 1 0 0 0 0
1 4 12 2 31 4 4 1 4 1 61 3 1 1 1 1 0 0 1 0 0 1 0 1 0
2 2 30 4 52 1 1 4 2 3 28 3 2 1 1 1 1 0 1 0 0 1 0 0 0
2 2 12 2 13 1 2 2 1 3 25 3 1 1 1 1 1 0 1 0 1 0 0 0 1
2 1 48 2 43 1 2 2 4 2 24 3 1 1 1 1 0 0 1 0 1 0 0 0 1
1 2 12 2 16 1 3 2 1 3 22 3 1 1 2 1 0 0 1 0 0 1 0 0 1
2 1 24 4 12 1 5 3 4 3 60 3 2 1 1 1 1 0 1 0 0 1 0 1 0
And my code is here:
for( int i = 0 ; i < linecount ; i++ )
{
string[] b = sr.ReadLine().Split( ' ' );
for( int j = 0 ; j < b.Length ; j++ )
{
a[i,j]= float.Parse( b[ j ]);
}
}
It gives me this error :
An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll
Additional information: Input string was not in a correct format.
Most of this issue on the internet is about having 123124.23234 instead of 123124,23234 but my numbers are integers. How can I do it? Thank you!
One of the items you are parsing to float is not just a number and may have spaces in it. You can do this to figure out which:
for (int i = 0; i < linecount; i++)
{
string[] b = sr.ReadLine().Split(' ');
for (int j = 0; j < b.Length; j++)
{
try
{
a[i, j] = float.Parse(b[j]);
}
catch (FormatException ex)
{
var notANumber = b[j];
}
}
}
You should instead try using:
float.Parse(b[j].Trim()); // Remove spaces and the parse it
That is if you are sure you may only have spaces and no other characters. If you have other characters, you need to remove them or replace them using Replace
. Better yet, you should check if the parse was successful then add it to the array.
Anyways you know what the issue is now.
We can filter out anything that isn't a number by using Regex, though I'm sure there are other alternatives too
const string testData = @"1 1 6 4 12 5 5 3 4 1 67 3 2 1 2 1 0 0 1 0 0 1 0 0 1
2 2 48 2 60 1 3 2 2 1 22 3 1 1 1 1 0 0 1 0 0 1 0 0 1
1 4 12 4 21 1 4 3 3 1 49 3 1 2 1 1 0 0 1 0 0 1 0 1 0
1 1 42 2 79 1 4 3 4 2 45 3 1 2 1 1 0 0 0 0 0 0 0 0 1
2 1 24 3 49 1 3 3 4 4 53 3 2 2 1 1 1 0 1 0 0 0 0 0 1
1 4 36 2 91 5 3 3 4 4 35 3 1 2 2 1 0 0 1 0 0 0 0 1 0
1 4 24 2 28 3 5 3 4 2 53 3 1 1 1 1 0 0 1 0 0 1 0 0 1
1 2 36 2 69 1 3 3 2 3 35 3 1 1 2 1 0 1 1 0 1 0 0 0 0
1 4 12 2 31 4 4 1 4 1 61 3 1 1 1 1 0 0 1 0 0 1 0 1 0
2 2 30 4 52 1 1 4 2 3 28 3 2 1 1 1 1 0 1 0 0 1 0 0 0
2 2 12 2 13 1 2 2 1 3 25 3 1 1 1 1 1 0 1 0 1 0 0 0 1
2 1 48 2 43 1 2 2 4 2 24 3 1 1 1 1 0 0 1 0 1 0 0 0 1
1 2 12 2 16 1 3 2 1 3 22 3 1 1 2 1 0 0 1 0 0 1 0 0 1
2 1 24 4 12 1 5 3 4 3 60 3 2 1 1 1 1 0 1 0 0 1 0 1 0";
float[] floatArray = testData.Split(' ')
.Select(s => Regex.Match(s, @"(\d*)").Groups[1].Value)
.Select(Convert.ToSingle)
.ToArray();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.