✔ 最佳答案
Method 1: use regular expression
import java.util.regex.*;
Pattern integerPattern = Pattern.compile("^\d*$");
Matcher matchesInteger = integerPattern.matcher(myString);
boolean isInteger = matchesInteger.matches();
or
boolean isInteger = Pattern.matches("^\d*$", myString);
Methid 2: Integer.parseInt() method
praseInt() will convert string to integer
int i;
bool isNumeric = false;
try
{
i = Integer.parseInt(myString);
isNumeric = true;
// did not throw error, myString is numeric
}
catch(NumberFormatException eX)
{
// threw an error, myString is not a number
isNumeric = false;
}