Difference between revisions of "2020 - Validation on entering text instead of numerical values"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "<syntaxhighlight lang=csharp> while (true) { Console.Write("Enter X coordinate for new outlet: "); try...")
 
Line 1: Line 1:
 
<syntaxhighlight lang=csharp>                 
 
<syntaxhighlight lang=csharp>                 
 
while (true)
 
while (true)
                {
+
{
                    Console.Write("Enter X coordinate for new outlet: ");
+
    Console.Write("Enter X coordinate for new outlet: ");
                    try
+
    try
                    {
+
    {
                        x = Convert.ToInt32(Console.ReadLine());
+
        x = Convert.ToInt32(Console.ReadLine());
                        break;
+
        break;
                    }
+
    }
                    catch
+
    catch
                    {
+
    {
                        Console.WriteLine("invalid data");
+
        Console.WriteLine("invalid data");
                    }
+
    }
                }
+
}
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 16:40, 9 December 2019

                
while (true)
{
    Console.Write("Enter X coordinate for new outlet: ");
    try
    {
        x = Convert.ToInt32(Console.ReadLine());
        break;
    }
    catch
    {
         Console.WriteLine("invalid data");
     }
}