Console Input and Output


This chapter already used System.Console.WriteLine repeatedly for writing out text to the command console. In addition to being able to write out data, a program needs to be able to accept data that a user may enter.

Getting Input from the Console

One of the ways to retrieve text that is entered at the console is to use System.Console.ReadLine(). This method stops the program execution so that the user can enter characters. When the user presses the Enter key, creating a newline, the program continues. The output, also known as the return, from the System.Console.ReadLine() method is the string of text that was entered. Consider Listing 1.13 and the corresponding output shown in Output 1.4.

Listing 1.13. Using System.Console.ReadLine()

class HeyYou {         static void Main()         {                 string firstName;                 string lastName;                 System.Console.WriteLine("Hey you!");                 System.Console.Write("Enter your first name: ");                 firstName = System.Console.ReadLine();                 System.Console.Write("Enter your last name: ");                 lastName = System.Console.ReadLine();                 ...        } }

Output 1.4.

 >HeyYou.exe Hey you! Enter your first name: Inigo Enter your last name: Montoya

After each prompt, this program uses the System.Console.ReadLine() method to retrieve the text the user entered and assign it to an appropriate variable. By the time the second System.Console.ReadLine() assignment completes, firstName refers to the value Inigo and lastName refers to the value Montoya.

Advanced Topic: System.Console.Read()

In addition to the System.Console.ReadLine() method, there is also a System.Console.Read() method. However, the data type returned by the System.Console.Read() method is an integer corresponding to the character value read, or 1 if no more characters are available. To retrieve the actual character, it is necessary to first cast the integer to a character, as shown in Listing 1.14.

Listing 1.14. Using System.Console.Read()

int readValue; char character; readValue = System.Console.Read(); character = (char) readValue; System.Console.Write(character);

The System.Console.Read() method does not return the input until the user presses the Enter key; no processing of characters will begin, even if the user types multiple characters before pressing the Enter key.


In C# 2.0, there is a new method called System.Console.ReadKey() which, in contrast to System.Console.Read(), returns the input after a single keystroke. It enables the developer to intercept the keystroke and perform actions, such as restricting the characters to numerics.

Writing Output to the Console

In Listing 1.13, you prompt the user for his first and last names using the method System.Console.Write() rather than System.Console.WriteLine(). Instead of placing a newline character after displaying the text, the System.Console.Write() method leaves the current position on the same line. In this way, any text the user enters will be on the same line as the prompt for input. The output from Listing 1.13 demonstrates the effect of System.Console.Write().

The next step is to write the values retrieved using System.Console.ReadLine() back to the console. In the case of Listing 1.15, the program writes out the user's full name. However, instead of using System.Console.WriteLine() as before, this code will use a slight variation. Output 1.5 shows the corresponding output.

Listing 1.15. Formatting Using System.Console.WriteLine()

class HeyYou {   static void Main()   {       string firstName;       string lastName;       System.Console.WriteLine("Hey you!");       System.Console.Write("Enter your first name: ");       firstName = System.Console.ReadLine();       System.Console.Write("Enter your last name: ");       lastName = System.Console.ReadLine();       System.Console.WriteLine("Your full name is {0} {1}.",                             firstName, lastName);                                                   } }

Output 1.5.

 Hey you! Enter your first name: Inigo Enter your last name: Montoya Your full name is Inigo Montoya

Instead of writing out Your full name is followed by another Write statement for firstName, a third Write statement for the space, and finally a WriteLine statement for lastName, Listing 1.15 writes out the entire output using composite formatting. With composite formatting, the code first supplies a format string to define the output format. In this example, the format string is "Your full name is {0} {1}.". It identifies two indexed placeholders for data insertion in the string.

Note that the index value begins at zero. Each inserted parameter (known as a format item) appears after the format string in the order corresponding to the index value. In this example, since firstName is the first parameter to follow immediately after the format string, it corresponds to index value 0. Similarly, lastName corresponds to index value 1.

Note that the placeholders within the format string need not appear in order. For example, Listing 1.16 switches the order of the indexed placeholders and adds a comma, which changes the way the name is displayed (see Output 1.6).

Listing 1.16. Swapping the Indexed Placeholders and Corresponding Variables

System.Console.WriteLine("Your full name is {1}, {0}",   firstName, lastName);

Output 1.6.

 Hey you! Enter your first name: Inigo Enter your last name: Montoya Your full name is Montoya, Inigo

In addition to not having the placeholders appear consecutively within the format string, it is possible to use the same placeholder multiple times within a format string. Furthermore, it is possible to omit a placeholder. It is not possible, however, to have placeholders that do not have a corresponding parameter.




Essential C# 2.0
Essential C# 2.0
ISBN: 0321150775
EAN: 2147483647
Year: 2007
Pages: 185

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net