0
string aresultlist = File.ReadAllLines(data).ToString();
var bresultlist = aresultlist.Split().Select(s => Convert.ToInt32(s));
List<int> resultlist = bresultlist.ToList(); 

Can anyone help with the FormatException I keep getting on this block, "It's the datetime Format, take date first exception". It's a string read from a space delimited text file.

live2
  • 3,771
  • 2
  • 37
  • 46
  • 1
    Why did you add `.ToString();` at the first line? How does your data look like? – Gilad Green Sep 27 '17 at 06:36
  • I added ToString because the data's formatted like this: – Mariah Nelson Sep 27 '17 at 06:39
  • Post sample of text. ReadAllLines() can cause big issues when parsing text files. You should read one line at a time using StreamReader(). I've been parsing text files for over 40 years and without seeing actual text I can't give good answer. – jdweng Sep 27 '17 at 06:39
  • 2
    `ReadAllLines` returns string array... using `ToString` to a string array may produce unexpected results, i.e. resulting type instead of value. – Tetsuya Yamamoto Sep 27 '17 at 06:40
  • It's not word wrapped here but:45 24 56 55 57 19 2 68 48 67 39 53 26 3 31 24 25 17 18 24 2 24 63 35 57 17 19 3 59 20 06 57 29 22 2 32 14 08 58 67 17 3 – Mariah Nelson Sep 27 '17 at 06:40

2 Answers2

2

Try this:

  List<int> resultList = File
    .ReadLines(data)                      // you've got IEnumerable<string>
    .Select(line => line.Split())         // -/- IEnumerable<string[]>
    .Select(ietms => int.Parse(items[0])) // -/- IEnumerable<int> 
    .ToList();                            // finally, it's List<int> 

I've assumed that it's the 1st item of the line which should be converted into int: int.Parse(items[0]), change 0 into the right index if required.

Try avoiding ReadAllLines in favor to ReadLines: you don't what all the file (which can be long) to be read into an array in one go

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • I'd recommend using `TryParse` as in [this question](https://stackoverflow.com/q/4961675/6400526). And if using C# 7.0 [this answer](https://stackoverflow.com/a/46150189/6400526) – Gilad Green Sep 27 '17 at 06:42
  • @Gilad Green: `TryParse` is a *better choice*, but it may be *too complex* for the author of the question (the very reason I've put *two* `Select` one after one) – Dmitry Bychenko Sep 27 '17 at 06:46
  • I agree :) And that is why I didn't post an answer of my own including it (together with you managing to post before me :) I just added the comment so author can read about it in addition to your good explanatory answer – Gilad Green Sep 27 '17 at 06:48
  • This still returns FormatException. Thanks, it's late where I'm at so I'll check on this in a few hours. – Mariah Nelson Sep 27 '17 at 06:50
  • @Mariah Nelson: `FormatException` means that we're trying to parse -`int.Parse(items[0])` - a string that's *not an integer*, say `"bla-bla-bla"`. Could you, please, *share your file* (or peace of it)? – Dmitry Bychenko Sep 27 '17 at 06:56
1

the problem is that ReadAllLines returns a string[]. If you call ToString on such an object you get the namespace.classname as a string. So in your case:

System.String[]

splitting this string results definetely not a number. But in a string[] with on entry, namely:

System.String[]

If your file has only one line with the space delimited numbers, I would suggest to use File.ReadAllText. It will read the entire content of the file and return it as 1 string. This way you can use your code almost as it is.

string aresultlist = File.ReadAllText(data);
var bresultlist = aresultlist.Split().Select(s => Convert.ToInt32(s));
List<int> resultlist = bresultlist.ToList(); 

EDIT:

As suggested by Gilad Green you might have content in the file that cannot be parsed to a number and will throw an exception. To avoid this you can follow this example

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
  • 1
    As in Dimtry's answer here too: I'd recommend using `TryParse` as in [this question](https://stackoverflow.com/q/4961675/6400526). And if using C# 7.0 [this answer](https://stackoverflow.com/a/46150189/6400526) – Gilad Green Sep 27 '17 at 06:46