title: How to check Type before casting in java
body: <p>I am casting my String variables to integer and double. I want to check whether the String variable contains valid Integer or Double value at runtime.</p>
  
  <p>I us following code but it not works for me.</p>
  
  <pre><code>String var1="5.5";
  String var2="6";
  Object o1=var1;
  Object o2=var2;
  if (o1 instanceof Integer)
  {
      qt += Integer.parseInt( var1);// Qty
  }
  if (o2 instanceof Double)
  {
      wt += Double.parseDouble(var2);// Wt
  }
  </code></pre>