0

I'm reading from a file with numbers and then when I try to convert it to an Int I get this error, System.FormatException: 'Input string was not in a correct format.' Reading the file works and I've tested all of that, it just seems to get stuck on this no matter what I try. This is what I've done so far:

StreamReader share_1 = new StreamReader("Share_1_256.txt");

string data_1 = share_1.ReadToEnd();

int intData1 = Int16.Parse(data_1);

Image showing the error I get.

If Parse is commented out it prints the data And then if parse is in it doesn't print anything.

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
Daniel
  • 5
  • 4
  • can you share stream data? – Md. Abdul Alim Mar 31 '22 at 07:39
  • 1
    Maybe the input string is not in the correct format? What is the string? Can you modify your code to not use a StreamReader but use the string that it reads directly? Do you know how to use a debugger? – Thomas Weller Mar 31 '22 at 07:39
  • 1
    How do you SURE `data_1` is number ? Are you print it on console ? – TimChang Mar 31 '22 at 07:42
  • @Thomas Weller I use data to read it and whatever is in the file is equal to data_1. I read online that StreamReader was an easier way of doing it all. Unless there is an easier way? I've never used a debugger before. – Daniel Mar 31 '22 at 07:43
  • @TimChang Yeah, it is. When I print to console it works fine. But when I try to make it int's it gives me an error. – Daniel Mar 31 '22 at 07:44
  • Can you post what you see on console ? @Daniel – TimChang Mar 31 '22 at 07:46
  • @TimChang I've got a picture in the post of what It looks like. When the parse is on it doesn't print anything and just throws an error and quits the console. If it is commented out then it prints it fine. – Daniel Mar 31 '22 at 07:54
  • Thanks @Daniel for the sharing of the console content. We see on it that this is not a number. So it is basically normal to have this error. I try to write down an answer – Dylan El Bar Mar 31 '22 at 07:55
  • You have a _list_ of ints, not a single int. Split by new lines and then parse each line – Jamiec Mar 31 '22 at 07:55
  • @Jamiec How would I split new lines and then parse each one? – Daniel Mar 31 '22 at 07:59
  • Please do not post images of errors, code, or text-data. – JonasH Mar 31 '22 at 08:04
  • @JonasH Tim just wanted to see what the console looked like. – Daniel Mar 31 '22 at 08:05
  • Of course, whatever is in the file will be in the string. But if the file content is "abc", then it will not be an int. – Thomas Weller Mar 31 '22 at 08:07

3 Answers3

1

As we can see in your post, your input file contains not one number but several. So what you will need is to iterate through all lines of your file, then try the parsing for each lines of your string.

EDIT: The old code was using a external library. For raw C#, try:

using (StringReader reader = new StringReader(input))
{
    string line;
    while ((line = reader.ReadLine()) != null)
    {
        // Do something with the line
    }
}

In addition, I encourage you to always parse string to number using the TryParse method, not the Parse one.

You can find some details and different implementations for that common problem in C#: C#: Looping through lines of multiline string

Dylan El Bar
  • 783
  • 1
  • 14
  • Why do you encuorage using "TryParse()"? Microsoft could simply have omitted "Parse()", but they haven't. – Thomas Weller Mar 31 '22 at 08:08
  • Because using Parse() method will throw an error in case of the Parsing fail. TryParse() will simply return a boolean indicating if yes or no the parsing has ended with success. The Parse method seems (to be confirmed) using the TryParse method internally and throw an exception if it returns false. So reason 1 is to avoid working with Exceptions (then Try...Catch), and reason 2 is performance. The first reason is applicable for beginners and it seems to be the case here. – Dylan El Bar Mar 31 '22 at 08:15
0

parser every single line

while (!reader.EndOfStream) 
{ 
    string line = reader.ReadLine(); 
    int intData1 = Int16.Parse(line);
} 
TimChang
  • 2,249
  • 13
  • 25
0

You can simplify the code and get rid of StreamReader with a help of File class and Linq:

// Turn text file into IEnumerable<int>:
var data = File
  .ReadLines("Share_1_256.txt")
  .Select(line => int.Parse(line));

//TODO: add .OrderBy(item => item); if you want to sort items

// Loop over all numbers within file: 15, 1, 48, ..., 32
foreach (int item in data) {
  //TODO: Put relevant code here, e.g. Console.WriteLine(item);
}
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215