0
0

Javaで「スキャナで取得した値を指定したデータ型の値として取得する」の動作を確認してみた

Posted at

概要

Javaで「スキャナで取得した値を指定したデータ型の値として取得する」の動作を確認してみました。以下のページを参考にしました。

実装

以下のファイルを作成しました。

JSample2_3.java
import java.util.Scanner;
import java.util.InputMismatchException;

class JSample2_3{
  public static void main(String[] args){
    Scanner scanner = new Scanner(System.in);

    try{
      System.out.println("年齢を入力してください");
      int old = scanner.nextInt(16);

      System.out.println("年齢は" + old + "です");
    }catch (InputMismatchException e){
      System.out.println("数値を入力してください");
    }
  }
}

以下のコマンドを実行しました。

$ javac JSample2_3.java 
$ java JSample2_3 
年齢を入力してください
2F
年齢は47です

まとめ

何かの役に立てばと。

0
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
0
0