473,387 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Beginner's question - Input String was not in correct format

Hi,

I am getting the error 'Input String was not in correct format' in the
following C# program (indicated line):

using System;
namespace CF7
{
class CalcArea
{
[STAThread]
static void Main(string[] args)
{
const decimal PI = 3.14159M;
int ch2;
float r;
do
{
Console.Write("Enter Radius: ");
r = Single.Parse(Console.ReadLine());
Console.WriteLine("The Area of circle is: {0}", (float)PI*r*r);
Console.Write("Calculate again (y/n): ");
ch2 = Int32.Parser(Console.ReadLine()); // error
} while((ch2 == 'y') || (ch2 == 'Y'));

}
}
}
[END]

Could please guide me to a solution and an understanding why I
encounter an exception?

Grateful
Mahmood Ahmad
Nov 16 '05 #1
3 3073
Mahmood Ahmad wrote:
Hi,

I am getting the error 'Input String was not in correct format' in the
following C# program (indicated line):

using System;
namespace CF7
{
class CalcArea
{
[STAThread]
static void Main(string[] args)
{
const decimal PI = 3.14159M;
int ch2;
float r;
do
{
Console.Write("Enter Radius: ");
r = Single.Parse(Console.ReadLine());
Console.WriteLine("The Area of circle is: {0}", (float)PI*r*r);
Console.Write("Calculate again (y/n): ");
ch2 = Int32.Parser(Console.ReadLine()); // error
} while((ch2 == 'y') || (ch2 == 'Y'));

}
}
}
[END]

Could please guide me to a solution and an understanding why I
encounter an exception?


Because Int32.Parse doesn't know what to do with the string "y" or "Y". A
quick, but hardly robust and free from pitfalls, fix would be:

using System;
namespace CF7
{
class CalcArea
{
[STAThread]
static void Main(string[] args)
{
const decimal PI = 3.14159M;
int ch2;
float r;
do
{
Console.Write("Enter Radius: ");
r = Single.Parse(Console.ReadLine());
Console.WriteLine("The Area of circle is: {0}", (float)PI*r*r);
Console.Write("Calculate again (y/n): ");
ch2 = Console.ReadLine()[0]; // Read just the first char entered and
behave accordingly
} while((ch2 == 'y') || (ch2 == 'Y'));
}
}
}

--
Tom Porterfield
MS-MVP MCE
http://support.telop.org

Please post all follow-ups to the newsgroup only.

Nov 16 '05 #2
The problem is that the Int32.Parse() method expects a string containing
numbers, not characters.
Try the following code instead.

Chris

static void Main(string[] args)
{
const decimal PI = 3.14159M;
string doAgain; // new line
float r;
do
{
Console.Write("Enter Radius: ");
r = Single.Parse(Console.ReadLine());
Console.WriteLine("The Area of circle is: {0}", (float)PI*r*r);
Console.Write("Calculate again (y/n): "); doAgain = Console.ReadLine().ToLower(); // new line } while(doAgain == "y") // modified
"Mahmood Ahmad" <ma*************@yahoo.com> wrote in message
news:83**************************@posting.google.c om... Hi,

I am getting the error 'Input String was not in correct format' in the
following C# program (indicated line):

using System;
namespace CF7
{
class CalcArea
{
[STAThread]
static void Main(string[] args)
{
const decimal PI = 3.14159M;
int ch2;
float r;
do
{
Console.Write("Enter Radius: ");
r = Single.Parse(Console.ReadLine());
Console.WriteLine("The Area of circle is: {0}", (float)PI*r*r);
Console.Write("Calculate again (y/n): ");
ch2 = Int32.Parser(Console.ReadLine()); // error
} while((ch2 == 'y') || (ch2 == 'Y'));

}
}
}
[END]

Could please guide me to a solution and an understanding why I
encounter an exception?

Grateful
Mahmood Ahmad

Nov 16 '05 #3
Thank you for your replies. Both the solutions worked.

However, Chris's solution produced a more desirable check on ch2. This
is because (as I understood it) the do-while loop repeats itself only if
the string ch2 is a one-character string "y". It won't repeat if ch2 is
composed of more than one character.

Thanx again,
Mahmood

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jim | last post by:
im using asp.net, C# to enter data into a table in sql server...however im getting this error: Input string was not in a correct format. Description: An unhandled exception occurred during the...
0
by: Alf P. Steinbach | last post by:
The seventh part of my attempted Correct C++ tutorial is now available, although for now only in Word format (use free Open Office if no Word), and also, it's not yet been reviewed at all -- ...
0
by: lianfe_ravago | last post by:
Input string was not in a correct format. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the...
5
by: blackg | last post by:
Input string not in correct format -------------------------------------------------------------------------------- I am trying to view a picture from a table. I am getting this error Input string...
0
by: hudhuhandhu | last post by:
have got an error which says Input string was not in a correct format. as follows.. Description: An unhandled exception occurred during the execution of the current web request. Please review the...
0
by: Anonieko | last post by:
Are there any javascript codes there? Answer: Yes On the PageLoad event call InitialClientControsl as follows /// <summary> /// This will add client-side event handlers for most of the...
0
by: sehguh | last post by:
Hiya Folks, I am Currently using windows xp. Also using Visual Web Developer 2005 and Microsoft Sql server 2005. The main page consists of an aspx page and a master page. The page also...
1
by: sehguh | last post by:
Hello folks I have recently been studying a book called "sams teach yourself asp.net 2.0 in24 hours by scott mitchell. I have reached page 614 but when i tried to run an asp page called...
1
by: differentsri | last post by:
THIS IS AN ASP.NET 1.1 APPLICATION IAM TRYING TO UPDATE THE FIELD BUT I AM NOT ABLE TO UPDATE IT? CAN U TELL THE REASON ? IT IS GIVING THE FOLLOWING ERROR BELOW I HAVE ALSO GIVEN THE CODE OF...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.