Javaで次のコードを実行すると:
import java.util.*;
class Solution{
public static void main(String []argh)
{
Scanner sc = new Scanner(System.in);
try{
long x=sc.nextLong();
System.out.println(x);
}
catch(Exception e){
System.out.println(sc.next()+" can't be fitted anywhere.");
}
sc.close();
}
}
入力を「23333333333333333333333333333333333333」と入力すると、次の出力が得られます。
23333333333333333333333333333333333333333 can't be fitted anywhere.
sc.nextLong()
スローされた場合、catch ブロックの は、try ブロックに入力された値とまったく同じ値をInputMismatchException
どのように取得しますか? ここでconsoleenterコードからの入力を求めるべきではありませんか?sc.next()
sc.nextLong()