title: Java Exception Handling to Check Primitive Types
body: <p>I have a GUI program that simulates a fuel station.</p>
  
  <p>In the program, there are 3 input fields:</p>
  
  <ul>
  <li>itemName</li>
  <li>number of Units(or volume in L)</li>
  <li>and amount in pence (per unit or litre).</li>
  </ul>
  
  <p>You can then chose to add the item by volume, or by units. The idea is that you can buy fuel and other items (such as food), with minimal input boxes.</p>
  
  <p>I'm using exception handling to check the input is what I want it to be:</p>
  
  <ul>
  <li>An <code>int</code> value to add by units</li>
  <li>and a <code>double</code> value to add by volume.</li>
  </ul>
  
  <p>My code so far recognises that a double has been entered where it wants an integer, and throws the error.</p>
  
  <p>For example, the input of: <code>item Name: Chocolate, Amount(or Litres): 2.5, Price: 85</code> gives the error: <code>The code used looks like this</code></p>
  
  <pre><code>if (e.getSource () == AddByNumOfUnits) {
      try {
          Integer.parseInt(NumOfUnitsField.getText());
      } catch (NumberFormatException exception) {
          SetErrorField("Input must be the appropriate type (real number for volume, integer for units)");
      }
  </code></pre>
  
  <p>However when adding by volume, I can't get the program to only accept <code>double</code> values, or anything that uses a decimal point. An <code>int</code> can be passed in and accepted as a <code>double</code> value, which I don't want. The code I'm using is very similar:</p>
  
  <pre><code>if (e.getSource() == AddByVolume) {
      try {
          double itemVolume = Double.parseDouble(NumOfUnitsField.getText());
      } catch (NumberFormatException exception) {
          SetErrorField("Input must be the appropriate type (real number for volume, integer for units)");
      }
  </code></pre>
  
  <p>If anyone could point me in the right direction of any way of solving this, that would be great.</p>
  
  <p>Thank you</p>