ZIP CODE VALIDATOR in C#- Can you help me, please? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

ZIP CODE VALIDATOR in C#- Can you help me, please?

I'm trying to solve Zip Code validator in Code coach (Easy). Looks like the if and else are right but how to implement the fact there can't be any letters? As I'm no PRO, I can't see all tests. I believe there's easier way to do the code for this but this is what's on my mind. :) Thank for anyone who could help me! <3 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Sololearn { class Program { static void Main(string[] args) { string num = Console.ReadLine(); int zip = Convert.ToInt32(num); if (zip >= 10000 && zip < 99999) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } } } }

26th Feb 2023, 12:57 AM
Tereza JW
Tereza JW - avatar
2 Answers
+ 2
ToInt32() will throw an exception if the conversion fails. Wrap your code in a try-catch and output "false" upon exception. That ought to do the trick. Incidentally, none of us can see hidden tests, not even PRO subscribers. The idea of hiding tests is that you cannot write your program to cover all tests without actually solving the problem.
26th Feb 2023, 6:03 AM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 2
Some zip codes have 0 as the leftmost digit. This code would wrongly claim those to be invalid codes.
26th Feb 2023, 1:43 PM
Brian
Brian - avatar