Topics covered:

  • Printing to the Console
    • Printing Strings and Numbers
  • Reading from the Console
    • Reading Characters
    • Reading Strings
    • Parsing Strings to Numeral Types
    • Reading Numeral Types
  • Various Examples

Video (in Bulgarian)

Presentation Content

Printing to the Console

Printing Strings, Numeral Types and Expressions

  • Console is used to display information in a text window
  • Can display different values:
    • Strings
    • Numeral types
    • All primitive data types
  • To print to the console use the class Console ( System.Console )

The Console Class

  • Provides methods for console input and output
    • Input
      • Read (…) – reads a single character
      • ReadKey(…) _ _ – reads a combination of keys
      • ReadLine(…) – reads a single line of characters
    • Output
      • Write(…) – prints the specified argument on the console
      • WriteLine(… ) – prints specified data to the console and moves to the next line

Formatting Strings

  • {index[,alignment][:formatString]}
  • index
    • The zero-based index of the argument whose string representation is to be included at this position in the string
  • alignment
    • A signed integer that indicates the total length of the field into which the argument is inserted
      • a positive integer – right-aligned
      • a negative integer – left-aligned
  • formatString
    • Specifies the format of the corresponding argument’s result string, e.g. " X ", " C ", " 0.00 "

Reading from the Console

Reading Strings and Numeral Types

  • We use the console to read information from the command line
  • We can read:
    • Characters
    • Strings
    • Numeral types (after conversion)
  • To read from the console we use the methods Console.Read() and Console.ReadLine()

Console.Read()

  • Gets a single character from the console (after [Enter] is pressed)
    • Returns a result of type int
    • Returns -1 if there aren’t more symbols
  • To get the actually read character we need to cast it to char

Console.ReadKey()

  • Waits until a combination of keys is pressed
    • Reads a single character from console or a combination of keys
  • Returns a result of type ConsoleKeyInfo
    • KeyChar – holds the entered character
    • Modifiers – holds the state of [Ctrl], [Alt], …

Console.ReadLine()

  • Gets a line of characters
  • Returns a string value
  • Returns null if the end of the input is reached

Reading Numeral Types

  • Numeral types can not be read directly from the console
  • To read a numeral type do the following:
    • Read a string value
    • Convert (parse) it to the required numeral type
  • int.Parse(string )
    • Parses (converts) a string to int

Converting Strings to Numbers

  • Numeral types have a method Parse(…) for extracting the numeral value from a string
    • int.Parse(string)string -> int
    • long .Parse(string )string -> long
    • float .Parse(string)string -> float
    • Causes FormatException in case of error
  • Converting can also be done using the methods of the Convert class
    • Convert.ToInt32(string)string -> int
    • Convert.ToSingle(string )string -> float
    • Convert.ToInt64(string)string -> long
    • It uses the parse methods of the numeral types

Error Handling when Parsing

  • Sometimes we want to handle the errors when parsing a number
    • Two options: use try - catch block or TryParse()
  • Parsing with TryParse()

How to Print Special Characters on the Console?

  • Printing special characters on the console needs two steps:
    • Change the console propertiesto enable Unicode-friendly font
    • Enable Unicode for the Consoleby adjusting its output encoding
      • Prefer UTF8 (Unicode)

Decimal Separator

  • The currency format and number formats are different in different countries
    • E.g. the decimal separator could be " . " or " , "
  • To ensure the decimal separator is “.” use the following code:

Summary

  • We have discussed the basic input and output methods of the class Console
    • Write(…) and WriteLine( )
      • Used to write values to the console
    • Read(…) and ReadLine( )
      • Used to read values from the console
  • Parsing numbers to strings
    • int.Parse(…) , double.Parse(…) , …