1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Scannerのnext()メソッドとnextLine()メソッドの違い

Posted at

競技プログラミングでよく使うScannerクラスの
next()メソッドとnextLine()メソッドの違いが理解できていなかったので、
調べてみました。

①next()メソッドの動き

入力

5
田中 太郎

コード

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int number = sc.nextInt();
        String lastName = sc.next();
        String firstName = sc.next();
        System.out.println(number);
        System.out.println(lastName);
        System.out.println(firstName);
        
    }
}

出力

5
田中
太郎

入力値1行目の数値5をnextInt()メソッドで取得し、
入力値2行目の文字列をnext()メソッドで取得しています。
1つ目のnext()メソッドは改行を無視して、入力値2行目1つ目の文字を取得しています。

next()デリミタ・パターンと一致する入力をスキップし、次のトークンを返そうとします。

※Oracle公式のJava APIドキュメントより
next()とhasNext()のメソッドとそのコンパニオン・メソッド(nextInt()やhasNextInt()など)は、まずデリミタ・パターンと一致する入力をスキップし、次のトークンを返そうとします。

つまり、空白や改行などのデリミタは無視して、次の文字列を取得してくれるのです。

②nextLine()メソッドの動き
nextLine()メソッドを次の行を取得すると勘違いしていた私。
予想としては
5
田中 太郎
と出力されることを予想していました。
しかし、下記のコードを実行すると、予想と違う結果になりました。

入力

5
田中 太郎

コード

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {	
        Scanner sc = new Scanner(System.in);
        int number = sc.nextInt();
        String name = sc.nextLine();
        System.out.println(number);
        System.out.println(name);
    }
}

出力

5

2行目の文字列が出力されていません。これはなぜでしょうか。

※Oracle公式のJava APIドキュメントより
public String nextLine()
スキャナを現在行の先に進めて、スキップした入力を返します。 このメソッドは、最後の行区切り文字を除く、現在行の残りを返します。 位置は、次の行の最初に設定されます。
このメソッドは行区切り文字の検索を入力内で続行するため、行区切り文字が存在しない場合、スキップする行を検索する入力をすべてバッファすることがあります。

戻り値:
スキップされた行

つまり、スキャナを次の入力に進め、スキップした、最後の行区切り文字を除く現在行の残りの入力を返すメソッドであるということです。

最初の入力値5をnextInt()メソッドで取得したのち、nextLine()メソッドを実行しています。
この時1行目にある値はすべて取得されているため、nextLine()メソッド改行文字のみが取得されることになり、結果として何も出力されていないように見えたわけです。

二行目を出力したければ、nextLine()メソッドの後にnextInt()メソッドを実行する必要があります。

入力

5
田中 太郎

コード

    import java.util.Scanner;
    
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int number = sc.nextInt();
        String name = sc.nextLine();
        String firstName = sc.next();
        String lastName = sc.next();
        System.out.println(number);
        System.out.println(name);
        System.out.println(firstName);
        System.out.println(lastName);     
    }
}

出力

5

田中
太郎

nextLine()メソッドには、改行が返されており、
その後next()メソッドを実行することで文字列を取得できたことが分かります。

◎まとめ

メソッド 取得内容・主な用途 主な用途
next() 次の「単語」 改行・空白で区切る 単語単位の入力
nextLine() 次の「1行全体」 改行までを読み込む 行全体の取得
1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?