Input And Output


Being able to collect input from the command prompt and display output in the command console is an integral part of Java's input/output functionality. Usually in Java you would have to create an instance of a java.io.BufferedReader object, using the System.in field in order to retrieve an input from the command prompt. The following code shows a simple Java class, JavaEcho, which takes input from the console and echoes it back, to illustrate the use of the Java.io package to gather and format input and output:

 import java.io.*; public class JavaEcho { public static void main(String[] args)throws IOException { BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); String userInput = stdin.readLine (); System.out.println ("You said: " + userInput); } } 

In C#, the System.Console class provides methods that can provide similar functionality for reading and writing from and to the command prompt. There is no need for any extra objects; the Console class provides methods that can read entire lines, read character by character, and even expose the underlying stream being read from. The members of Console are briefly described in the following tables.

Public Properties

Description

Error

Gets the system's standard error output stream as a TextWriter object.

In

Gets the system's standard input stream as a TextReader object.

Out

Gets the system's standard output stream as a TextWriter object.

Public Methods

Description

OpenStandardError()

Overloaded. Returns the standard error stream as a Stream object.

OpenStandardInput()

Overloaded. Returns the standard input stream as a Stream object.

OpenStandardOutput()

Overloaded. Returns the standard output stream as a Stream object.

Read()

Reads the next character from the standard input stream.

ReadLine()

Reads the next line of characters as a string from Console, which is set to the system's standard input stream by default.

SetError()

Redirects the Error property to use the specified TextWriter stream.

SetIn()

Redirects the In property to use the specified TextReader stream.

SetOut()

Redirects the Out property to use the specified TextWriter stream.

Write()

Overloaded. Writes the specified information to Console.Out.

WriteLine()

Overloaded. Writes information followed by a line terminator to Console.Out.

All of the Console members are static, so you don't need to (and can't) instantiate a System.Console object.

Using the powerful methods of the Console class, we could write an equivalent of the JavaEcho class in C# as follows:

 class CSEchoer { static void Main(string[] args) { string userInput = System.Console.ReadLine(); System.Console.WriteLine ("You said : " + userInput); } } 

This code is much shorter and easier to digest in comparison to its Java counterpart. One useful thing you'll get with the Console.WriteLine() static method is the ability to use formatted strings. The flexibility of formatted strings can be illustrated by writing a simple game where user input is used to generate a story. Here is the code for this game, EchoGame:

 class EchoGame { static void Main(string[] args) { System.Console.WriteLine("Name of a country?"); string userInput1 = System.Console.ReadLine(); System.Console.WriteLine("Name of a young prince?"); string userInput2 = System.Console.ReadLine(); System.Console.WriteLine("What was the prince doing?"); string userInput3 = System.Console.ReadLine(); System.Console.WriteLine("What did he find while doing this?"); string userInput4 = System.Console.ReadLine(); System.Console.WriteLine("Then what did he do?"); string userInput5 = System.Console.ReadLine(); System.Console.WriteLine("Once upon a time in" + " {0}, there was a young prince {1},\n" + "who while {2}, came across a {3}, and then " + "{4} ! ", userInput1, userInput2, userInput3, userInput4, userInput5 ); } } 

The insertion points are replaced by the supplied arguments starting from the index {0}, which corresponds to the leftmost variable (in this case userInput1). You are not limited to supplying only string variables, nor are you confined to using just variables, or even using variables of the same type. Any type that the method WriteLine() can display can be supplied as an argument, including string literals or actual values. There is also no limit to the number of insertion points that can be added to the string, as long as it is less than the overall number of arguments. Note that omitting insertion points from the string will cause the variable not to be displayed. You must, however, have an argument for each insertion point you specify whose index in the argument list corresponds to the index of the insertion point. In the following listing, for example, removing {1} is fine as long as there are still three arguments. In this case {0} matches up with strA and {2} matches up with strC:

 Console.WriteLine("hello {0} {1} {2}", strA, strB, strC); 




Professional C# 2005
Pro Visual C++ 2005 for C# Developers
ISBN: 1590596080
EAN: 2147483647
Year: 2005
Pages: 351
Authors: Dean C. Wills

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