charles
charles

Reputation: 97

How can I align the text with the text in the next line in console?

is there a shortcut method for aligning the text in the first line with the second line in console?

example:

Enter your name: Charles

Enter your student number: 20130140

//and the output should be like this:

   Name      Student Number
  Charles       20130140

Here is my code but it's in a long method.

class MainClass
{
        public static void Main (string[] args)
        {
            int num, nam;
            string snum, name, sec;
            int num1, num2, num3;
            int var;
            int bosh;

        Console.WriteLine ("Enter student number: ");
        snum = Console.ReadLine ();
        Console.WriteLine ("Enter Name: ");
        name = Console.ReadLine ();
        Console.WriteLine ("Enter Section: ");
        sec = Console.ReadLine ();

        num1 = snum.Length;
        num2 = name.Length;
        num3 = sec.Length;

        Console.WriteLine ("        Student Number        Name        Section        ");
        Console.Write("\n");

        if (num1 <= 14) {

            num = (14 - num1) / 2;
            var = (14 - num1) - num;

            for (int i = 0; i < num; i++) {

                Console.Write (" ");
            }
            Console.Write ("        " + snum);

            if (num2 <= 4) {
                nam = (4 - num2) / 2;
                //sum of all sapaces
                bosh = var + nam + 8;

                for (int i = 0; i < bosh; i++) {

                    Console.Write (" ");
                }
                Console.Write (name);
            } else {
                nam = ( num2 - 4) / 2;
                //sum of all sapaces
                bosh = var + (8 - nam);

                for (int i = 0; i < bosh; i++) {

                    Console.Write (" ");
                }
                Console.Write (name);

            }


        } else if (num1 > 14){

            num = 8 - ((num1 - 14) / 2);

            for (int i = 0; i < num; i++) {

                Console.Write (" ");
            }
            Console.Write (snum);

            if (num2 <= 4) {
                nam = (4 - num2) / 2;
                //sum of all sapaces
                bosh = nam + num;

                for (int i = 0; i < bosh; i++) {

                    Console.Write (" ");

                }

                Console.Write (name);

            } else {

                nam = (num2 - 4) / 2;
                //sum of all sapaces
                bosh = num - nam;

                for (int i = 0; i < bosh; i++) {

                    Console.Write (" ");

                }

                Console.Write (name);

            }

        }

        Console.ReadKey ();

    }

}

Upvotes: 1

Views: 922

Answers (1)

Panagiotis Kanavos
Panagiotis Kanavos

Reputation: 131189

The format strings used in .NET accept an alignment component that allow you to specify the length of an argument and its alignment, eg:

Console.WriteLine("{0,5} {1,-10}",5,10);

will use 5 spaces for the first argument, right aligned and 10 spaces for the second argument, left aligned.

In your case you could write something like:

Console.WriteLine("{0,-25} {1,-15} {2,10}",name,number,section);

This will output name in columns 1-25 left aligned, number in 27-42 left aligned and section in 44 right aligned to 10 characters

The tricky part is that if the length of the formatted string is greater than the alignment length, the alignment is ignored. You can address that by first formatting each element before the final output statement, truncating them to the maximum allowed length.

Unfortunately, there is no way to center align the arguments. You'd have to calculate the length of each formatted string and pad it in code. The answers to this SO question use either a function to center-align the arguments, or a custom IFormattable that center-aligns the arguments of the format string.

Upvotes: 2

Related Questions