概要
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です
まとめ
何かの役に立てばと。