3 Replies - 871 Views - Last Post: 20 March 2012 - 12:24 PM Rate Topic: -----

#1 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,693
  • Joined: 13-March 10

Input string not in correct format

Posted 20 March 2012 - 12:04 PM

I have the following code and at line:

int number = Convert.ToInt32((string)tokens[i]);

I get errror:

Format Exception was unhandled, Input string was not in a correct format.

The file has format:

1 2 4 5 6
45 33

etc etc

 public static void Main(string[] args)
    {
        int N = 8;
        int rows = 0;
        int cols = 0;
        int[,] a = new int[N, N];
        TextReader reader = new StreamReader("C:/Project/file_8.txt");
        string line = reader.ReadLine();
        System.Console.WriteLine(line);
        while (line != null)
        {
            string[] tokens = line.Split('\t');
            for (int i = 0; i < tokens.Length; i++)
            {
                int number = Convert.ToInt32((string)tokens[i]);
                a[rows, cols] = number;
                cols++;
            }
            cols = 0;
            rows++;
            line = reader.ReadLine();
            System.Console.WriteLine(line);
        }
    }



Is This A Good Question/Topic? 0
  • +

Replies To: Input string not in correct format

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Input string not in correct format

Posted 20 March 2012 - 12:13 PM

Set a breakpoint at line 15. Step through your code until the exception is thrown and see what the value is that is causing the problem.
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5632
  • View blog
  • Posts: 12,080
  • Joined: 02-June 10

Re: Input string not in correct format

Posted 20 March 2012 - 12:18 PM

First... you can't turn "two" into an integer of 2.
"2 " {note the space} can't become 2.
You have to programmatically strip out all the non-numerics before you try to parse a string to an int.

Second... your path string is wrong.

TextReader reader = new StreamReader(@"C:\Project\file_8.txt");



View PostRyano121, on 20 March 2012 - 01:13 PM, said:

Set a breakpoint at line 15. Step through your code until the exception is thrown and see what the value is that is causing the problem.


Good advice Ryano121 but there is no need to give darek9576 advice about how to debug. He already knows all about it.

I also offered him some links to tutorials on debugging but was informed he didn't need them.

View Postdarek9576, on 20 March 2012 - 12:11 PM, said:

Dont assume things. I am familiar with breakpoints and debugging.

Was This Post Helpful? 1
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5632
  • View blog
  • Posts: 12,080
  • Joined: 02-June 10

Re: Input string not in correct format

Posted 20 March 2012 - 12:24 PM

Darek, since you already know about how to debug and use breakpoints it would help us to help you narrow down the problem if you could take a screen capture at the time the error is thrown, showing us the Locals pallet so we can see the actual values of the variables in question.

I would have assumed it to be this line:

15 int number = Convert.ToInt32((string)tokens[i]);


And that tokens[i] is equal to " " since you said the file contained spaces between the numbers, and you're not checking for that possibility.

Quote

The file has format:

1 2 4 5 6
45 33


But placing a breakpoint there while debugging would have already shown you this... So the problem must be something else and without more details I'm not sure how to offer assistance.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1