LoginSignup
1
1

More than 5 years have passed since last update.

標準入力を都度取得する方法

Last updated at Posted at 2016-11-16

Scannerクラスを使うことで標準入力を取得することができます。

Scannerクラスのメソッドを紹介します。

入力された値の認識する範囲を識別するメソッド

  next:入力された値を”空白”までの範囲で認識
  nextLine:入力された値を”改行”までの範囲で認識

  Scanner sc = new Scanner(System.in);
  ①String a = sc.next("test test");
  ②String b = sc.nextLine("test test");

例の結果ですが、
nextの場合は空白までの値が認識されるためaには”test”が代入されることになります。
nextLineは空白も文字列として認識されるためbには”test test”が代入されます。

ちなみに型指定して読み込むことが可能です。
   nextInt:入力値をint型で読み込む
   nextDouble:入力値をDouble型で読み込む

1
1
2

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
1