Click here to Skip to main content
15,887,898 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This code is showing error
C#
System.FormatException: Input string was not in a correct format

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("hello");
            Console.WriteLine("enter the character");
            int c = Console.Read();
            Console.WriteLine(c);

            Console.WriteLine("please enter first integer");
            int p1 = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("second");
            int p2 = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine(p1 + p2);
          
        }
    }
}


But if i simple comment the code as below it is working fine:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //Console.WriteLine("hello");
            //Console.WriteLine("enter the character");
            //int c = Console.Read();
            //Console.WriteLine(c);

            Console.WriteLine("please enter first integer");
            int p1 = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("second");
            int p2 = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine(p1 + p2);
          
        }
    }
}


Please tell me the reason.
Thanks


What I have tried:

int p1 = Convert.ToInt32(Console.ReadLine().Trim());
but it also showing the same error.
Posted
Updated 11-Jun-16 20:53pm

You are inputing a character and reading it as int. That's the problem. Correct it.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 11-Jun-16 16:10pm    
5ed, but the code does need a correction. :-)
Please see Solution 3.
—SA
It's not a problem with the code. The problem is with the input you're trying to feed this line of code. Whatever you're typing at the console cannot be converted to an integer.

You might want to look into Int32.TryParse() instead. Read up on it in the documentation first.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 11-Jun-16 16:11pm    
5ed, but "not a problem with the code" is a slight exaggeration... :-)
—SA
In addition to the Solutions 1 and 2:

Don't use the class Convert when it's not really needed. This is not "conversion"; this is parsing. Better use the method which does exactly that, in your case, int.Parse, or, better, the method not throwing the exception, int.TryParse:
https://msdn.microsoft.com/en-us/library/system.int32.tryparse%28v=vs.110%29.aspx.

—SA
 
Share this answer
 
v2
I copied you code and tested it.
even though i input the integer values it was giving the same format exception..
Consider Solution 1,2,3 for information regarding Parsing Integer values

the problem is that, you are using Console.Read() method

Quote:
The Read method blocks its return while you type input characters; it terminates when you press the Enter key. Pressing Enter appends a platform-dependent line termination sequence to your input (for example, Windows appends a carriage return-linefeed sequence). Subsequent calls to the Read method retrieve your input one character at a time. After the final character is retrieved, Read blocks its return again and the cycle repeats.


Refer Console.Read Method (System)[^]

try debugging this below code

C#
class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("hello");
            Console.WriteLine("enter the character");
            int c = Console.Read();
            Console.WriteLine(c);

            Console.WriteLine("please enter first integer");
            string p1str = Console.ReadLine(); // this will be ignored if you use Console.Read()
            int p1 = Convert.ToInt32(p1str);
            Console.WriteLine("second");
            string p2str = Console.ReadLine();
            int p2 = Convert.ToInt32(p2str);
            Console.WriteLine(p1 + p2);

        }
    }


The Console.Read() will be assigning the last char value in that line.
and the immediate Console.ReadLine() will be ignored from the User Input and it assigns the value which is already typed on the screen.
for example:
if you type "a1" then the p1str will be holding the value "1" by ignoring the first char
if you type "123" then the p1str will be holding the value "23" which can be parsed to integer without exception.
if you type "1[pressed enter key]" then p1str will be holding the value "" (empty or return key ) which will result in format exception..

I hope you understood, apply breakpoint and debugger, witness it how it works..
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900