0

Need help with finding the null, I cant quite figure it out for this one. Specifically checking to find a null and then putting the output saying that's a null

public int EXP;

public static void Main(string[] args)
{
    Console.Write("Check to see if its a prime number!strong text Enter a number! ");

    int Vo = Convert.ToInt32(Console.ReadLine());
    int Va = Check_Prime(Vo);

    if (Va == 0)
    {
        Console.WriteLine("not a prime number!", Vo);
    }
    else
    {
        Console.WriteLine("Is a prime number!", Vo);
    }

    Console.Read();
}

private static int Check_Prime(int Vo)
{
    int L;

    for (L = 2; L <= Vo - 1; L++)
    {
        if (Vo % L == 0)
        {
            return 0;
        }
    }

    if (L == Vo)
    {
        return 1;
    }
    return 0;
}
2
  • What do you mean by "find the null"?? Are you getting a null reference exception? Commented Mar 1, 2018 at 1:38
  • When you hit ENTER, Console.ReadLine returns empty string. It doesn't return null. It will return null only if you press CTRL + Z. Commented Mar 1, 2018 at 2:55

3 Answers 3

1

Do a check before your integer conversion, else it will return 0 for null.

string userInput = Console.ReadLine();
if(string.IsNullOrEmpty(userInput))
{
    Console.WriteLine("Your message");
}

int Vo = Convert.ToInt32(userInput);
// Rest of your code
Sign up to request clarification or add additional context in comments.

1 Comment

I think you should check for IsNullOrWhiteSpace
0

I think apart from checking for null or empty, you should also check for non-numerical values.

public static void Main(string[] args)
{
    Console.Write("Check to see if its a prime number!strong text Enter a number! ");

    string number = Console.ReadLine();
    if(string.IsNullOrWhiteSpace(number))
    {
        Console.WriteLine("Input is empty.");
        return;
    }
    int Vo;
    bool success = int.TryParse(number, out Vo);
    if(!success)
    {
        Console.WriteLine("Input is not an integer.");
        return;
    }
    int Vo = Convert.ToInt32();
    int Va = Check_Prime(Vo);

     if (Va == 0)
     {
        Console.WriteLine("not a prime number!", Vo);
     }
    else
    {
        Console.WriteLine("Is a prime number!", Vo);
    }

    Console.Read();
}

You could also check if the input is an integer and less than 0 and show error message as prime numbers are positive numbers only afaik. But that will be an additional check.

Comments

0

If you want to ensure that the user enters a valid integer, one way would be to get the user input in a loop whose exit condition is that the entry is a valid int. I usually do this by making use of a helper method. The helper method takes in a string "Prompt", which is displayed to the user before getting their input.

The helper method makes use of the int.TryParse method, which takes in a string value (in our example, I use Console.ReadLine() directly for this, since it returns a string), and it takes an out int parameter, which will be set to the integer representation of the string if it's successful. The method itself returns true if the conversion was successful:

private static int GetIntFromUser(string prompt)
{
    int value;

    do
    {
        Console.Write(prompt);
    } while (!int.TryParse(Console.ReadLine(), out value));

    // If TryParse succeeds, the loop exits and 'value' contains the user input integer
    return value;
}

The beauty of this is that the method handles all the retrying, and all you have to do from your main code is call it with the prompt you want to give the user, and it's guaranteed that the input will be an integer:

public static void Main(string[] args)
{
    Console.WriteLine("I will tell you if the number you enter is a prime number!\n");
    int Vo = GetIntFromUser("Enter a whole number: ");

With just that last line above, if the user tries to enter an invalid number (or nothing at all), the program just keeps asking them until they comply:

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.