I am new to this forum and also programming only in my second week at uni so thanks in advance for your help and pateince
It is only a simple program and i am trying to confirm the reason for this error ( pasted at the bottom)
I assume it is due to the incorrect format of the string.
All i am trying to achieve is validation of user input when/if invalid year was entered.
If letters are entered it crashes the program and generates the error.
And if an incorrect year is entered it will only allow the user to enter a year 1 more time, and if that is incorrect it will not repeat until correct year is entered.
Any help would be greatly appreciated.
Thanks
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Prac1._1
{
class Program
{
static void Main(string[] args)
{
int Year;
string Title;
string Publisher;
int Length ;
bool repeat = true;
while (repeat == true)
{
Console.WriteLine("\nMovie Information! Press 'enter' to continue...");
Console.ReadLine();
Console.Write("Input Movie Title: ");
Title = Console.ReadLine();
Console.WriteLine("Movie Title: {0} ", Title);
{
Console.WriteLine("\nInput Year Released: ");
string input = Console.ReadLine();
Year = Int32.Parse(input); // Format Exception was unhandled (appears on this line)
if (Year < 1886 || Year > 2012)
{
Console.WriteLine("No movies were made before 1886 or after 2011. Please try again.");
Console.WriteLine("\nInput Year Released: ");
Console.ReadLine();
}
else
{
Console.WriteLine("Year Released: {0} ", Year);
}
}
Console.Write("\nInput Publisher: ");
Publisher = Console.ReadLine();
Console.WriteLine("Publisher: {0} ", Publisher);
Console.Write("\nInput Length (hrs): ");
Length = Convert.ToInt32(null); Console.ReadLine();
Console.WriteLine("Length (hrs): {0} ", Length);
Console.WriteLine("Go again? Y/N");
string go = Console.ReadLine();
if (go == "Y" || go == "y")
{
repeat = true;
}
else
{
repeat = false;
Console.WriteLine("Press any key to exit.");
}
}
}
}
}
Error:
System.FormatException was unhandled
Message=Input string was not in a correct format.
Source=mscorlib
StackTrace:
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.Int32.Parse(String s)
at Prac1._1.Program.Main(String[] args) in \\vmware-host\Shared Folders\Desktop\Visual Projects\Prac1.1\Prac1.1\Program.cs:line 35
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: