Standard IO in Java

Many operating systems allow programs to mention a standard source of input (called standard input) and a standard place for output to go (called standard output). By default, keyboard activity goes into the standard input and standard output goes to (a window on) the screen.

It is easy to command the operating system to get standard input for a program from somewhere else (like a file) or to send standard output somewhere else.

Input/Output (IO) is quite a complicated business (with ends of lines and invisible characters etc) but programming languages usually provide simple facilities for standard IO.

Java allows you to just write System.out.println("a line"); to send a line to the standard output.

So (unless you redirect standard output), the output will go to the screen (and be ready to put any further output on the next line).

Use System.out.print("a bit of a line"); if you don't want to finish a line.

Starting with J2SE 5.0 (JDK 1.5), System.out.printf method is available to display formatted output, eg
System.out.printf("%s\n%s\n", "Welcome to ICT306 !", "The Unit Coordinator is Pyara Dhillon.");
System.out.printf("Today's maximum forecast temperature is %d\n", temp);

Standard Input

Standard input will come into a program from the keyboard (unless you redirect it).

Java has been unusually inconvenient when it comes to standard input.

This is because Java has strict rules for error handling. Inputs can cause errors (like a letter arriving when a number is expected) and so these errors should be handled. Java expects to be told what to do if an input error arises. You, the programmer, should be telling the program what to do.

Anyway, these days, most serious programs use GUIs for IO (and this is quite easy in Java). Later in the course we will see how to set up simple GUI input.

Until 2004,  introductory text books and introductory Java courses have usually supplied their own facilities for standard input.
Starting with Java version 5.0 (JDK 1.5), the new Scanner class is available for input. This class must be imported in your java program as shown in the following example:

//New_IO.java
// uses new class Scanner available in jdk1.5/1.6 to read input
import java.util.Scanner;   // scanner class must be imported
public class New_IO{
public static void main(String[] args){
    Scanner keyboard = new Scanner(System.in);
    String a="";
    while (true){
    //an infinite loop, use Ctrl-C to quit
        System.out.println("Enter a line:");
        a= keyboard.nextLine();  // read a string
        System.out.println("Your line:");
        System.out.println(a);
        System.out.println();
    }
//end of while
}//end of main
}//end of class

Not a good design to trap a user in an infinite loop but as a user (and debugger) of your own programs, remember to use Ctrl-C (i.e press the Ctrl and C keys together) to quit such a program.

Other methods of Scanner class include:
 next(), nextInt(), nextFloat(), nextDouble to read a word (string), an integer, a float, and a double respectively. See the class documentation for full details.