Topic 3: Input & Output using standard device

print statement

The print() method is used to send data to a standard output display device. Information that is sent to the screen can be any combination of literal constants, named constants, and variables.

Following is a syntax for writing a print statement:

Syntax:
  1. System.out.print([data to be displayed]);
Example: statement to display Hello There world!
  • System.out.print("Hello World!");

println() method moves the cursor to the next line on the screen. It's the same as pressing the Enter key on the keyboard

Example
  • System.out.println("Hello World! ");
    System.out.print("Hello Java! ");
    System.out.println("I will print on the same line.");
Output
  • Hello World!
    Hello Java! I will print on the same line.

Escape sequence

The escape sequences are special non-printing characters that are used to control the printing behaviour of the output stream objects (such as ‘cout’). The escape sequences and the characters they represent are given in Table 3.1:

Table 3.1
Escape sequence Character represented
\a Alert (bell, alarm)
\b Backspace
\f Form feed (new page)
\n New-line
\r Carriage return
\t Horizontal tab
\v Vertical tab
\’ Single quotation mark
\” Double quotation mark
\? Question mark
\\ Backslash

Input from the keyboard

In Java, there are many ways to read strings from input. The simplest one is to make use of the class Scanner , which is part of the java.util library and has been newly introduced in Java 5.0. Using this class, we can create an object to read input from the standard input channel System.in (typically, the keyboard), as follows:

  1. Scanner scanner = new Scanner(System.in);

Then, we can use the nextLine() method of the Scanner class to get from standard input the next line (a sequence of characters delimited by a newline character), according to the following schema:

Example
  • ...
    Figure 3.1
  • import java.util.Scanner; - imports the class Scanner from the library java.util
  • Scanner scanner = new Scanner(System.in); - creates a new Scanner object, that is connected to standard input (the keyboard)
  • String inputString = scanner.nextLine();
    1. temporarily suspends the execution of the program, waiting for input from the keyboard;
    2. the input is accepted as soon as the user digits a carriage return;
    3. (a reference to) the string entered by the user (up to the first newline) is returned by the nextLine() method;
    4. (the reference to) the string is assigned to the variable inputString.
Java Scanner Methods to Take Input

The Scanner class provides various methods that allow us to read inputs of different types.

Table 3.1
Method Description
nextInt() reads an int value from the user
nextFloat() reads a float value form the user
nextBoolean() reads a boolean value from the user
nextLine() reads a line of text from the user
next() reads a word from the user
nextByte() reads a byte value from the user
nextDouble() reads a double value from the user
nextShort() reads a short value from the user
nextLong() reads a long value from the user