Input Validations in C# at C Sharp for Beginners Course Codeasy.Net

Write a program that validates a battleship move. It first reads a letter [A–J] or [a–j] and then an integer [1–10], each from the new line. If the input for the move is valid, program should output “Next move is {letter}{number}.” Otherwise, it should output “Letter should be from A to J. Number from 1 to 10. Try again!” The program should continue to ask for and read values for the battleship move until the user inputs valid values for both of them. Pay attention: it should be case insensitive; ‘A’ and ‘a’ are both allowable. Your program should, however, output letter in the same case as it was entered. For example:

a

Letter should be from A to J. Number from 1 to 10. Try again!

10
a
Letter should be from A to J. Number from 1 to 10. Try again!

10
Letter should be from A to J. Number from 1 to 10. Try again!

c
5
Next move is c5.

using System;

namespace InputValidation
{
    class Battleship
    {
        static void Main(string[] args)
        {
            var letterAsString = Console.ReadLine();
            var numberAsString = Console.ReadLine();

            char letter;
            int number;

            while (!char.TryParse(letterAsString, out letter) || !int.TryParse(numberAsString, out number) || letter < 'a' || letter > 'j' || number < 1 || number > 10)
            {

                Console.WriteLine("Letter should be from A to J. Number from 1 to 10. Try again!");
                letterAsString = Console.ReadLine();
                numberAsString = Console.ReadLine();
            }

            Console.WriteLine($"Next move is {letter}{number}.");
        }
    }
}

Can’t seem to find a way to make it case insensitive. Any help?

You are on the right track! I think the condition for while should be a bit more complete: either it is a capital letter or a lower case one.
while (... !( (l >= 'a' && l <= 'j') || (l >='A' && l <= 'J' )) ...)

I think something like this will do. Basically, it checks that the letter is NOT either lowercase or an upper case

Please let me know if it worked!

using System;

namespace InputValidation
{
    class Battleship
    {
        static void Main(string[] args)
        {
            var letterAsString = Console.ReadLine();
            var numberAsString = Console.ReadLine();

            char letter;
            int number;

            while (!char.TryParse(letterAsString, out letter) || !int.TryParse(numberAsString, out number) || !((letter >= 'a' && letter <= 'j') || (letter >= 'A' && letter <= 'J')))
            {

                Console.WriteLine("Letter should be from A to J. Number from 1 to 10. Try again!");
                letterAsString = Console.ReadLine();
                numberAsString = Console.ReadLine();
            }

            Console.WriteLine($"Next move is {letter}{number}.");
        }
    }
}

The program works as intended, I have tried all combinations and didn’t find any errors, but codeasy still doesn’t accept it

It seems to me that you are not checking the condition that the number should be from 1 to 10. I entered
> c
> 22

And the output of the program was:

Next move is c22.