Console Input / Output: Reading and Writing To The Console
Console Input / Output: Reading and Writing To The Console
Imran Khan
Printi Table of Contents
ng to
the
Conso
le
Pr
in
ti
ng
St
ri
ng
s
2
Conso Printing to the Console
le is
used
to
displa
y
infor
matio
n in a
text
windo
w
Can 3
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
4
Console.Write(…)
Printing an integer variable
int a = 15;
...
Console.Write(a); // 15
7
Printing a Menu – Example
double colaPrice = 1.20;
string cola = "Coca Cola";
double fantaPrice = 1.20;
string fanta = "Fanta Dizzy";
double zagorkaPrice = 1.50;
string zagorka = "Zagorka";
Console.WriteLine("Menu:");
Console.WriteLine("1. {0} – {1}",
cola, colaPrice);
Console.WriteLine("2. {0} – {1}",
fanta, fantaPrice);
Console.WriteLine("3. {0} – {1}",
zagorka, zagorkaPrice);
Console.WriteLine("Have a nice day!");
8
Reading from the Console
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()
9
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
int i = Console.Read();
char ch = (char) i; // Cast the int to char
10
Waits Console.ReadKey()
until a
combi
nation
of
keys is
presse
d
Read
sa
ConsoleKeyInfo key = Console.ReadKey();
singl
Console.WriteLine();
Console.WriteLine("Character entered: " + key.KeyChar);
e
Console.WriteLine("Special keys: " + key.Modifiers);
char
acter 11
Console.ReadLine()
Gets a line of characters
Returns a string value
Returns null if the end of the input is reached
12
Reading Numeral Types
Numeral types can not be read directly from the
console
To read a numeral type do the following:
1. Read a string value
2. Convert (parse) it to the required numeral type
int.Parse(string) – parses a string to int
string str = Console.ReadLine()
int number = int.Parse(str);
13
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
string s = "123";
int i = int.Parse(s); // i = 123
long l = long.Parse(s); // l = 123L
float f = float.Parse(Console.ReadLine());
Console.WriteLine("{0} * {1} / {2} = {3}",
a, b, f, a*b/f);
}
15
Converting Strings to
Numbers (2)
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
Internally uses the parse methods of the numeral
types
string s = "123";
int i = Convert.ToInt32(s); // i = 123
long l = Convert.ToInt64(s); // l = 123L
string invalid = "xxx1845";
int value = Convert.ToInt32(invalid); // FormatException
16
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():
string str = Console.ReadLine();
int number;
if (int.TryParse(str, out number))
{
Console.WriteLine("Valid number: {0}", number);
}
else
{
Console.WriteLine("Invalid number: {0}", str);
}
17
Printing a Letter – Example
Console.WriteLine(" Yours,");
Console.WriteLine(" {0}", company);
18
Calculating Area – Example
Console.WriteLine("This program calculates " +
"the area of a rectangle or a triangle");
19
We Summary
have
discus
sed
the
basic
input
and
outpu
t
meth
ods of
the 20
1. Write Exercises
a
progra
m that
reads
3
intege
r
numb
ers
from
the
consol
e and
prints 21
4. Write Exercises (2)
a
progra
m that
reads
two
positiv
e
intege
r
numb
ers
and
prints
how 22
7. Write Exercises (3)
a
progr
am
that
gets
a
numb
er n
and
after
that
gets
more
n 23