Skip to content
Advertisement

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 is an index for instructions array whis is array for parsed program I wanna check if the element I’m at its index is a numeric so I can perform the addittion normally else I’m gonna call my system caller

Advertisement

Answer

You can check like this:

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