このコードは問題なく、うまく動作します。
public static void main(String[] args) {
String [] keywords={"auto","break","case","char","const","continue","default",
"do","double","else","enum","extern","float","for","goto","if","int",
"long","register","return","short","signed","sizeof","static","struct",
"switch","typedef","union","unsigned","void","volatile","while" };
Scanner sn = new Scanner (System.in);
if(Arrays.asList(keywords).contains(sn.next())) {
System.out.println("This is a KEYWORD");
}
しかし、これを追加すると
else if(sn.hasNextInt()){
System.out.println("This is an INTEGER");
}
実行すると、入力スキャナーが停止しません。そのため、結果が得られません。なぜこうなった?
説明付きの解決策を教えてください。前もって感謝します。