【Java】標準入力を取得するコードまとめ

## 環境
[paiza.IO](https://paiza.io/ja)

## はじめに
この記事では、Javaの標準入力を取得するコードを入力の種類別に紹介する。

:::note
`nextInt()` は状況に応じて `nextLong()` , `nextDouble()` 等に変更する。→ [メソッド一覧](https://docs.oracle.com/javase/jp/11/docs/api/java.base/java/util/Scanner.html#Scanner.3TFECy)
:::

## 文字列（1行）
```:入力例
abc
```

### Scanner.next()
```java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
    }
}
```


## 文字列（複数行）
```:入力例
3
abc
def
ghi
```

### Scanner.next()
```java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        final int N = sc.nextInt();
        String[] wordArray = new String[N];
      	for(int i = 0; i < N; i++) {
      		wordArray[i] = sc.next();
      	}
    }
}
```

:::note
`sc.nextInt()` は `Integer.parseInt(sc.next())`（こちらの方が高速） でも可
:::

## 半角スペース区切りの文字列（1行）
```:入力例
abc def ghi
```

### Scanner.nextLine().split(" ")
```java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] wordArray = sc.nextLine().split(" ");
    }
}
```

### Scanner.next()
```java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        final int N = 3;  // 文字列の数
        String[] wordArray = new String[N];
        for(int i = 0; i < N; i++) {
          wordArray[i] = sc.next();
        }
    }
}
```

## 半角スペース区切りの文字列（複数行）
```:入力例
2 3
abc def ghi
jkl mno pqr
```

### Scanner.next()
```java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        final int H = sc.nextInt();
        final int W = sc.nextInt();
        String[][] wordArray = new String[H][W];
      	for(int i = 0; i < H; i++) {
        	for(int j = 0; j < W; j++) {
        		wordArray[i][j] = sc.next();
        	}
      	}
    }
}
```

:::note
`sc.nextInt()` は `Integer.parseInt(sc.next())`（こちらの方が高速） でも可
:::

### Scanner.nextLine().split(" ")
```java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        final int H = sc.nextInt();
        final int W = sc.nextInt();
        sc.nextLine();
        String[][] wordArray = new String[H][W];
      	for(int i = 0; i < H; i++) {
      	  wordArray[i] = sc.nextLine().split(" ");
      	}
    }
}
```

:::note
`sc.nextInt()` は `Integer.parseInt(sc.next())`（こちらの方が高速） でも可
:::

## 数値（1行）
```:入力例
100
```

### Scanner.nextInt()
```java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
    }
}
```

### Integer.parseInt(Scanner.next())
```java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.next());
    }
}
```

## 数値（複数行）
```:入力例
3
100
200
300
```

### Scanner.nextInt()
```java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        final int N = sc.nextInt();
        int[] numArray = new int[N];
      	for(int i = 0; i < N; i++) {
      		numArray[i] = sc.nextInt();
      	}
    }
}
```

### Integer.parseInt(Scanner.next())
```java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        final int N = Integer.parseInt(sc.next());
        int[] numArray = new int[N];
      	for(int i = 0; i < N; i++) {
      		numArray[i] = Integer.parseInt(sc.next());
      	}
    }
}
```

## 半角スペース区切りの数値（1行）
```:入力例
100 200 300
```

### Scanner.nextInt()
```java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        final int N = 3;  // 数値の数
        int[] numArray = new int[N];
    	for(int i = 0; i < N; i++) {
    		numArray[i] = sc.nextInt();
    	}
    }
}
```

### Integer.parseInt(Scanner.next())
```java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        final int N = 3;  // 数値の数
        int[] numArray = new int[N];
    	for(int i = 0; i < N; i++) {
    	    numArray[i] = Integer.parseInt(sc.next());
    	}
    }
}
```

### Scanner.nextLine().split(" ") + Integer.parseInt()

```java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        final int N = 3;  // 数値の数
        int[] numArray = new int[N];
        String[] strArray = sc.nextLine().split(" ");
    	for(int i = 0; i < N; i++) {
    		numArray[i] = Integer.parseInt(strArray[i]);
    	}
    }
}
```

## 半角スペース区切りの数値（複数行）
```:入力例
2 3
100 200 300
400 500 600
```

### Scanner.nextInt()
```java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        final int H = sc.nextInt();
        final int W = sc.nextInt();
        Integer[][] numArray = new Integer[H][W];
      	for(int i = 0; i < H; i++) {
        	for(int j = 0; j < W; j++) {
        		numArray[i][j] = sc.nextInt();
        	}
      	}
    }
}
```

### Integer.parseInt(Scanner.next())
```java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        final int H = Integer.parseInt(sc.next());
        final int W = Integer.parseInt(sc.next());
        Integer[][] numArray = new Integer[H][W];
      	for(int i = 0; i < H; i++) {
        	for(int j = 0; j < W; j++) {
        		numArray[i][j] = Integer.parseInt(sc.next());
        	}
      	}
    }
}
```

### Scanner.nextLine().split(" ") + Integer.parseInt()
```java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        final int H = Integer.parseInt(sc.next());
        final int W = Integer.parseInt(sc.next());
        sc.nextLine();
        int[][] numArray = new int[H][W];
        String[] strArray;
      	for(int i = 0; i < H; i++) {
      	    strArray = sc.nextLine().split(" ");
            for(int j = 0; j < W; j++) {
        		numArray[i][j] = Integer.parseInt(strArray[j]);
            }
      	}
    }
}
```

## 参考

https://rnsr0371.boy.jp/2020/10/09/java_stdin/

https://qiita.com/p_shiki37/items/a0f6aac33bf60f5f65e4

http://rishirishi.livedoor.blog/archives/10123493.html

https://wata-orz.hatenadiary.org/entry/20090914/1252902159

https://jpliterature.hatenablog.com/entry/java-method-of-console-input-output