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:println() method moves the cursor to the next line on the screen. It's the same as pressing the Enter key on the keyboard
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:
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 |
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:
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:
The Scanner class provides various methods that allow us to read inputs of different types.
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 |