Home > Enterprise >  How can remove the word "null" in the output FIRSTNAME: NULL?
How can remove the word "null" in the output FIRSTNAME: NULL?

Time:02-20

        string FirstName, LastName = " ";
        char MiddleInitial = ' ';

        Console.Write("Insert  first name: ");
        FirstName = Console.ReadLine();

        if (FirstName.Equals("null"))
        {
            Console.Write("Insert middle initial ");
            Console.Write("\nInsert last name \n");
        }
        else
        {
            Console.Write("Insert middle initial: ");
            MiddleInitial = Console.ReadLine()[0];
            Console.Write("Insert  last name: ");
            LastName = Console.ReadLine();
        }

        string Month, Year, Day;
        Console.Write("\nInsert birth month: ");
        Month = Console.ReadLine();

        if (Month.Equals("null"))
        {
            Console.Write("Insert birth day ");
            Console.Write("\nInsert birth year \n");

            Console.WriteLine("\nYour record");
            Console.WriteLine("FIRST NAME:{0}", FirstName);
            Console.WriteLine("MIDDLE INITIAL: {0}", MiddleInitial);
            Console.WriteLine("LASTNAME: {0}", LastName);
            Console.WriteLine("BIRTH MONTH:");
            Console.WriteLine("BIRTH DAY:");
            Console.WriteLine("BIRTH Year:");
            Console.WriteLine("BIRTHDAY:");
          
            Console.ReadKey();
        }
        else
        {
            Console.Write("Insert birth day: ");
            Day = Console.ReadLine();
            Console.Write("Insert birth year: ");
            Year = Console.ReadLine();

            Console.WriteLine("\nYour record");
            Console.WriteLine("FIRST NAME:{0}", FirstName);
            Console.WriteLine("MIDDLE INITIAL: {0}", MiddleInitial);
            Console.WriteLine("LASTNAME: {0}", LastName);
            Console.WriteLine("BIRTH MONTH: {0}", Month);
            Console.WriteLine("BIRTH DAY: {0}", Day);
            Console.WriteLine("BIRTH YEAR: {0}", Year);
            Console.WriteLine("BIRTHDAY:{0}/{1}/{2}", Month, Day, Year);

            Console.ReadKey();
        }

The program runs correctly the only matter is how can I make the program output to be like this:

Insert  first name: null
Insert middle initial
Insert last name

Insert birth month: null
Insert birth day
Insert birth year

Your record
FIRST NAME:
MIDDLE INITIAL:
LASTNAME:
BIRTH MONTH:
BIRTH DAY:
BIRTH Year:
BIRTHDAY:

But what the program currently outputs is this:

Insert  first name: null
Insert middle initial
Insert last name

Insert birth month: null
Insert birth day
Insert birth year

Your record
FIRST NAME:null
MIDDLE INITIAL:
LASTNAME:
BIRTH MONTH:
BIRTH DAY:
BIRTH Year:
BIRTHDAY:

As you can see it display

FIRST NAME:null    // It should be empty

The program should display nothing if I insert two "null" words without sacrificing others function.

But if I insert one null and then I insert the value of the others should display like this

Insert  first name:James
Insert middle initial:F
Insert last name:Capili

Insert birth month:null
Insert birth day
Insert birth year

Your record
FIRST NAME:James
MIDDLE INITIAL:F
LASTNAME:Capili
BIRTH MONTH:
BIRTH DAY:
BIRTH Year:
BIRTHDAY:

Insert  first name: null
Insert middle initial
Insert last name

Insert birth month: 04
Insert birth day:14
Insert birth year:2000

Your record
FIRST NAME:
MIDDLE INITIAL:
LASTNAME:
BIRTH MONTH:04
BIRTH DAY:14
BIRTH Year:2000
BIRTHDAY:04/14/2000

I've been thinking this like for 4 hours trying to figure out what best logic I should do. I hope someone can help me.

CodePudding user response:

  • Handle the null value through FirstName == "null" ? "" : FirstName

      using System;
    
      class Program
      {
      public static void data()
      {
          string FirstName, LastName = " ";
          char MiddleInitial = ' ';
    
      Console.Write("Insert  first name: ");
      FirstName = Console.ReadLine();
    
      if (FirstName.Equals("null"))
      {
          Console.Write("Insert middle initial ");
          Console.Write("\nInsert last name \n");
    
      }
      else
      {
          Console.Write("Insert middle initial: ");
          MiddleInitial = Console.ReadLine()[0];
          Console.Write("Insert  last name: ");
          LastName = Console.ReadLine();
      }
      string Month, Year, Day;
      Console.Write("\nInsert birth month: ");
      Month = Console.ReadLine();
    
      if (Month.Equals("null"))
      {
          Console.Write("Insert birth day ");
          Console.Write("\nInsert birth year \n");
    
          Console.WriteLine("\nYour record");
          Console.WriteLine("FIRST NAME:{0}", FirstName == "null" ? "" : FirstName);
          Console.WriteLine("MIDDLE INITIAL: {0}", MiddleInitial);
          Console.WriteLine("LASTNAME: {0}", LastName);
          Console.WriteLine("BIRTH MONTH:");
          Console.WriteLine("BIRTH DAY:");
          Console.WriteLine("BIRTH Year:");
          Console.WriteLine("BIRTHDAY:");
    
    
          Console.ReadKey();
      }
      else
      {
          Console.Write("Insert birth day: ");
          Day = Console.ReadLine();
          Console.Write("Insert birth year: ");
          Year = Console.ReadLine();
    
          Console.WriteLine("\nYour record");
          Console.WriteLine("FIRST NAME:{0}",  FirstName == "null" ? "" : FirstName);
          Console.WriteLine("MIDDLE INITIAL: {0}", MiddleInitial);
          Console.WriteLine("LASTNAME: {0}", LastName);
          Console.WriteLine("BIRTH MONTH: {0}", Month);
          Console.WriteLine("BIRTH DAY: {0}", Day);
          Console.WriteLine("BIRTH YEAR: {0}", Year);
          Console.WriteLine("BIRTHDAY:{0}/{1}/{2}", Month, Day, Year);
    
    
          Console.ReadKey();
      }
    
     }
      public static void Main(string[] args)
      {
          data();
      }
     }
    
  •  Tags:  
  • c#
  • Related