# Java Input/Output

## Input

### Read a single character

Using **System.in.read()**:

```java
import java.io.IOException;

char character = (char) System.in.read(); // make sure that the method containing this statement has "throws IOException" in its signature, e.g. public static void main(String[] args) throws IOException { }
```

### Read a single word

Using **next()** method of **Scanner**:

```java
import java.util.Scanner;

Scanner input = new Scanner(System.in);
String word = input.next();
```

### Read a line of text

Using **nextLine()** method of **Scanner**:

```java
// input = new Scanner(System.in);
String line = input.nextLine();
```

### Read a boolean

Using **nextBoolean()** method of **Scanner** - only works if input is true and false, not 0 and 1:

```java
// input = new Scanner(System.in);
boolean bool = input.nextBoolean();
```

### Read a whole number

Using one of **nextByte()**, **nextShort()**, **nextInt()**, or **nextLong()** methods of **Scanner**:

```java
// input = new Scanner(System.in);
byte b = input.nextByte();
short s = input.nextShort();
int i = input.nextInt();
long l = input.nextLong(); // input shouldn't end with L, e.g. 4672L will throw InputMismatchException
```

For differences between these data types, refer to the [Primitive Data Types Cheatsheet](https://fosseryweb.codeberg.page/@beta/cheatsheets/java/primitive-data-types.html)

### Read a decimal number

Using one of **nextFloat()** or **nextDouble()** methods of **Scanner**:

```java
// input = new Scanner(System.in);
float f = input.nextFloat(); // input shouldn't end with f, e.g. 4672.234f will throw InputMismatchException
double d = input.nextDouble(); // input shouldn't end with d, e.g. 4672.234d will throw InputMismatchException

```

For differences between these data types, refer to the [Primitive Data Types Cheatsheet](https://fosseryweb.codeberg.page/@beta/cheatsheets/java/primitive-data-types.html)

> **Warning!** After calling System.in.read() or one of the next...() methods of Scanner, the provided input might not be fully read, unread characters remain in the input stream, resulting in unexpected behavior (e.g. a user prompting skipped, or InputMismatchException thrown). To avoid these kind of issues, call the nextLine() method of Scanner (without using its return value) after each call of the mentioned methods, to read out the remaining content of the input stream.

## Output

### Print a line of text

Using **System.out.println()** - also works with primitive data types and arrays:

```java
System.out.println("This is a line of text");
System.out.println("This is another line of text");
/*
  This is a line of text
  This is another line of text
*/
```

### Print multiple pieces of text into the same line

Using **System.out.print()** - also works with primitive data types and arrays:

```java
System.out.print("This is some text ");
System.out.print("This is some more text in the same line");
// This is some text This is some more text in the same line
```

### Print formatted text

Using **System.out.printf()**:

```java
System.out.printf("Hello %s!", "Fossery"); // Hello Fossery!
System.out.printf("Hello %S!", "Fossery"); // Hello FOSSERY!

System.out.printf("This statement is %b, because...", true); // This statement is true, because...
System.out.printf("This statement is %B, because...", true); // This statement is TRUE, because...

System.out.printf("The result is %d", 123456); // The result is 123456
System.out.printf("The result is %,d", 123456); // The result is 123,456

System.out.printf("The result is %f", 123456.789); // The result is 123456.789000
System.out.printf("The result is %.3f", 123456.789); // The result is 123456.789
System.out.printf("The result is %.0f", 123456.789); // The result is 123457
System.out.printf("The result is %#.0f", 123456.789); // The result is 123457.

System.out.printf("The result is %e", 123456.789); // The result is 1.234568e+05
System.out.printf("The result is %.3e", 123456.789); // The result is 1.235e+05
System.out.printf("The result is %E", 123456.789); // The result is 1.234568E+05
System.out.printf("The result is %.3E", 123456.789); // The result is 1.235E+05

System.out.printf("The character with 100 Unicode code points is %c", 100); // The character with 100 Unicode code points is d
System.out.printf("The character with 100 Unicode code points is %C", 100); // The character with 100 Unicode code points is D
```
