20
23

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

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

Last updated at Posted at 2022-09-27

環境

paiza.IO

はじめに

この記事では、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]);
            }
      	}
    }
}

参考

20
23
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
23

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?