UNB/ CS/ David Bremner/ teaching/ cs2999/ io

Help with IO

For all problems, your program should take its input from standard in, and print its output in standard out. All of the input will be made up of text characters: letters, symbols or numbers. If you already know how to handle input and output in the language of your choice, you can skip this section. However, if you’re not sure, or are having difficulties, this section is here to help you get past the nuts and bolts of I/O and into the actual problem solving.



For text input and output, which is all you need, the simplest thing to do is use a BufferedReader. A BufferedReader is constructed with an InputStreamReader, which is constructed with an InputStream, such as System.in (Standard in). You must import java.io.*, as in the following example:

import java.io.*;
public class IOTest {
  public static void main(String[] args) throws IOException {
    BufferedReader in = new BufferedReader(new
    //Code to read in data will follow...

The basic methods you may use for input:

int read()

String readLine()

A typical problem for contestants using Java is that there may be extra whitespace (spaces or tabs) at the end or beginning the line. The trim() method of the string class can help by removing any extra whitespace.


Printing to standard out is done via the usual System.out.print() and System.out.println() methods.


You need to #include <stdio.h> to use these functions:


getchar() ~ gets the ASCII value for the next character in standard in.

scanf() ~ takes several parameters: A format specifying string (see format below), followed by pointers to variables that you want to store the data.


 char c = getchar();
 scanf("%d %d", \&x, \&y);


putchar(char c) ~ puts the character specified by ’c’ to standard out.

printf() ~ takes one to several parameters: A content/format specifying string, followed by values to insert into the format specifying string.


printf("x equals %d \n", x);

Format Specifying

Both printf and scanf use the following format specifiers.

  %d int

  %c char

  %f float

  %lf double (long float)

  %s string


You need to #include <iostream> to use these functions, using namespace std:

#include <iostream>
using namespace std;


cin >> var;

The ">>" operator is overloaded to work as the reader for cin, standard in. You can read most basic data types this way, from chars, ints, floats and doubles, as well as STL strings. This will skip over all intervening whitespace until it has read the proper data type, if it can. It returns 0 if it could not read the data (probably because it reached the end of the input).


peek() returns the value of the next character in the input, but does not read it. get() returns the same value, but reads it and advances down the input stream. So get() will munch through the input one char at a time, where peek() just looks ahead.


cout << output << andMore << " output." << endl;

You can concatenate your output as above. This will work for most basic data types, including STL strings. The "endl" is a variable which will print out the proper end of line characters for the system the program is running on.