如何检查字符串是否为数字,例如“1”是但 "x" 不是

How to check if a string is a numeric or not for example "1" is but "x" is not

static int Add() {
  Instindex++;
  if ()

    int a = Integer.parseInt(instructions[Instindex]);
  else
    ReadInstruction();
  Instindex++;
  if (x == (int) x)
    int b = Integer.parseInt(instructions[Instindex]);
  else
    ReadInstruction();
  return a + b;
}

instindex 是指令数组的索引 whis 是已解析程序的数组我想检查我在其索引处的元素是否为数字,以便我可以正常执行加法,否则我将调用我的系统调用程序

public static boolean isNumeric(String str) {
  return str.matches("-?\d+(\.\d+)?");  //match a number with optional '-' and decimal.
}

试试这个

您可以这样检查:

boolean isNumeric(String str){
    try{
        Integer.parseInt(str);
        return true;
    }catch (NumberFormatException e){
        return false;
    }
}