環境
はじめに
この記事では、Javaの標準入力を取得するコードを入力の種類別に紹介する。
nextInt()
は状況に応じて nextLong()
, nextDouble()
等に変更する。→ メソッド一覧
文字列(1行)
入力例
abc
Scanner.next()
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()
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();
}
}
}
sc.nextInt()
は Integer.parseInt(sc.next())
(こちらの方が高速) でも可
半角スペース区切りの文字列(1行)
入力例
abc def ghi
Scanner.nextLine().split(" ")
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()
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()
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();
}
}
}
}
sc.nextInt()
は Integer.parseInt(sc.next())
(こちらの方が高速) でも可
Scanner.nextLine().split(" ")
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(" ");
}
}
}
sc.nextInt()
は Integer.parseInt(sc.next())
(こちらの方が高速) でも可
数値(1行)
入力例
100
Scanner.nextInt()
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())
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()
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())
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()
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())
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()
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()
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())
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()
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]);
}
}
}
}
参考