スキャナーで作成した文字列を使用して、スペースで区切られたリストをループし、各要素が整数であることを確認します。すべてパスした場合は true を返します。それ以外の場合は false を返します。
isInteger チェックの功績は、Java で文字列が整数かどうかを判断する
public static boolean containsAllIntegers(String integers){
String[] newArray = integers.split(" ");
//loop over the array; if any value isnt an integer return false.
for (String integer : newArray){
if (!isInteger(integer)){
return false;
}
}
return true;
}
public static boolean isInteger(String s) {
try {
Integer.parseInt(s);
} catch(NumberFormatException e) {
return false;
}
// only got here if we didn't return false
return true;
}