Resources‎ > ‎Java Basics‎ > ‎

Command Line IO

Output

The standard mechanism for command line output in Java looks as follows:
System.out.println("stuff to print");
This code invokes the println method of the object out that is a static data member of the class System.  The object out is of type java.io.PrintStream.  PrintStream provides both a println method and a print method.  Both methods can take as input strings (as shown above) or ints, chars, doubles, floats, longs, and booleans.  The print method prints the data and does not advance to the next line.  The println method prints the data given and then prints a newline. 
/*
    Output of the following is:

Hello there, Bob
Good to see you!
*/
String name = "Bob";
System.out.print("Hello ");
System.out.println("there, " + name);
System.out.println("Good to see you!");

The code above demonstrates the difference between print and println.  It also demonstrates string concatenation.  Two strings can be concatenated using the + operator.

Input

Command line input in Java is a bit tricky.  The easiest way to achieve this is by using the Scanner class.  An example follows:
Scanner scan = new Scanner(System.in);
System.out.println("Enter some text: ");
String str1 = scan.nextLine();
System.out.println("Enter some more text: ");
String str2 = scan.nextLine();

A few things to note:
  • The Scanner constructor takes as input an InputStream.  To get input from the command line, pass it the in object that is a static data member of the System class.
  • The nextLine method of Scanner takes no input and returns the next line of text, ending with a newline.
  • Once you have created a Scanner object, you can use it repeatedly to get input.
  • At the top of the class that includes these lines of code, you must import the Scanner class.  This means that you explicitly tell the compiler that you will be using this class you can do this as follows:
import java.util.Scanner;
Scanner scan = new Scanner(System.in);
System.out.println("Enter a number: ");
int x = scan.nextInt();
System.out.println("Enter another number: ");
int y = scan.nextInt();

The Scanner also provides method nextInt, nextDouble, and so on.  You can use these methods to retrieve data of a particular type.

It becomes a bit tricky, however, to retrieve different types of data. 
Scanner scan = new Scanner(System.in);
System.out.println("Enter a number: ");
int x = scan.nextInt();
System.out.println("Enter some text: ");
String s = scan.nextLine();

In the example above, the scanner will retrieve the number entered by the user, but will leave a newline character in the buffer.  When the scanner retrieves the next line of text, it thinks that there is already a line in the buffer, and will not wait for the user to enter new data. 

One way to deal with this is to clear the buffer after the nextInt call by calling nextLine until the method hasNext of the Scanner returns false.  Another option is to read all input as text and convert Strings to ints or doubles where appropriate.
Comments