For the C++ answer, look at this question which has already solved a quite similar problem, that you can very easily adapt to your situation.
As for Java, you can do this :
public boolean isInteger(String s) {
return s.matches("^[0-9]+$");
}
You can modify the regex to suite your requirements. For example : "^[4-8]+$".
Note: String.matches is not optimal. If you need to perform checks often, use a compiled pattern instead :
static final Pattern DIGITS = Pattern.compile("^[0-9]+$");
public void isInteger(String s) {
return DIGITS.matcher(s).find();
}