title: Java applet program to calculate the KwH Used, Electric Bill, System Charges and Total Bill in a certain place. (Electric billing system)
body: <p>Can someone help me to determine what is the problem here? I'm trying to compile a java applet program. But the problem is whenever I try to click the &quot;compute&quot; or &quot;clear&quot; button, nothing seems to work. I'm trying to change my conditions to try catch form but I don't know how can I implement it. This is the system's condition:</p>
  <p><a href="https://pasteboard.co/JQ7sEaD.png" rel="nofollow noreferrer">https://pasteboard.co/JQ7sEaD.png</a></p>
  <p>And here is my own code:</p>
  <pre><code>import java.applet.Applet;
  import java.awt.*;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.awt.event.ItemEvent;
  import java.awt.event.ItemListener;
  import javax.swing.JOptionPane;
  
  public class ElectricReading extends Applet implements ActionListener, ItemListener
  {
      Label txt = new Label (&quot;Area:&quot;);
      CheckboxGroup area = new CheckboxGroup();
      Checkbox df = new Checkbox(&quot;Default&quot;,true,area);
      Checkbox mm = new Checkbox(&quot;Metro Manila Area&quot;,false,area);
      Checkbox pa = new Checkbox(&quot;Provincial Area&quot;,false,area);
      
      Label an = new Label(&quot;Account Number:&quot;);
      Label cn = new Label(&quot;Customer Name:&quot;);
      Label pr = new Label(&quot;Previous Reading:&quot;);
      Label cr = new Label(&quot;Current Reading:&quot;);
      Label ku = new Label(&quot;KwH Used:&quot;);
      Label at = new Label(&quot;Account Type:&quot;);
      Choice ch = new Choice();
      Label eb = new Label(&quot;Electric Bill:&quot;);
      Label sc = new Label(&quot;System Charges:&quot;);
      Label tb = new Label(&quot;Total Bill:&quot;);
      
      TextField anTF = new TextField(&quot; &quot;, 20);
      TextField cnTF = new TextField(&quot; &quot;, 20);
      TextField prTF = new TextField(&quot; &quot;, 20);
      TextField crTF = new TextField(&quot; &quot;, 20);
      TextField kuTF = new TextField(&quot; &quot;, 25);
      TextField ebTF = new TextField(&quot; &quot;, 20);
      TextField scTF = new TextField(&quot; &quot;, 20);
      TextField tbTF = new TextField(&quot; &quot;, 20);
  
      Button comp = new Button(&quot;Compute&quot;);
      Button clr = new Button(&quot;Clear&quot;);
  
      
      public void init()
      {
          resize(350,500);
          
          add(txt);
          add(df); df.addItemListener(this);
          add(mm); mm.addItemListener(this);
          add(pa); pa.addItemListener(this);
          
          add(an); add(anTF);
          add(cn); add(cnTF);
          add(pr); add(prTF);
          add(cr); add(crTF);
          add(ku); add(kuTF); kuTF.setEditable(false);
          add(at); add(ch);
          ch.add(&quot; &quot;);
          ch.add(&quot;Residential&quot;);
          ch.add(&quot;Commercial&quot;);
          ch.add(&quot;Industrial&quot;);    
          add(eb); add(ebTF); ebTF.setEditable(false);
          add(sc); add(scTF); scTF.setEditable(false);
          add(tb); add(tbTF); tbTF.setEditable(false);
          
          add(comp); comp.addActionListener(this);
          add(clr); clr.addActionListener(this);
          
      }
      
      public void itemStateChanged(ItemEvent e)
      {
          float prevReading = Float.parseFloat(prTF.getText());
          float currReading = Float.parseFloat(crTF.getText());
          int r = 15, c = 30, i = 45;
          Choice ch = (Choice) e.getItemSelectable();
          
          if(currReading &gt; prevReading)
          {
              float ku = currReading - prevReading;
              kuTF.setText(String.valueOf(ku));
          }
          else
          {
              JOptionPane.showMessageDialog(null, &quot;Incorrect Reading&quot;, &quot;Message&quot;, JOptionPane.INFORMATION_MESSAGE);
          }
          
          if(mm.getState() &amp;&amp; ch.getSelectedItem().equals(&quot;Residential&quot;))
          {
              float eb = (ku * r);
              ebTF.setText(String.valueOf(eb));
              float sc = (eb * 0.03);
              scTF.setText(String.valueOf(sc));
          }
          else if(mm.getState() &amp;&amp; ch.getSelectedItem().equals(&quot;Commercial&quot;))
          {
              float eb = (ku * c);
              ebTF.setText(String.valueOf(eb));
              float sc = (eb * 0.06);
              scTF.setText(String.valueOf(sc));
          }
          else if(mm.getState() &amp;&amp; ch.getSelectedItem().equals(&quot;Industrial&quot;))
          {
              float eb = (ku * i);
              ebTF.setText(String.valueOf(eb));
              float sc = (eb * 0.09);
              scTF.setText(String.valueOf(sc));
          }
          if(pa.getState() &amp;&amp; ch.getSelectedItem().equals(&quot;Residential&quot;))
          {
              float eb = (ku * r);
              ebTF.setText(String.valueOf(eb));
              float sc = (eb * 0.02);
              scTF.setText(String.valueOf(sc));
          }
          else if(pa.getState() &amp;&amp; ch.getSelectedItem().equals(&quot;Commercial&quot;))
          {
              float eb = (ku * c);
              ebTF.setText(String.valueOf(eb));
              float sc = (eb * 0.04);
              scTF.setText(String.valueOf(sc));
          }
          else if(pa.getState() &amp;&amp; ch.getSelectedItem().equals(&quot;Industrial&quot;))
          {
              float eb = (ku * i);
              ebTF.setText(String.valueOf(eb));
              float sc = (eb * 0.06);
              scTF.setText(String.valueOf(sc));
          }
              
      }
  
  public void actionPerformed(ActionEvent e)
  {
      try
      {
      if(e.getSource()==comp)
          {
          Choice ch = (Choice) e.getItemSelectable();
              if(ch.getSelectedItem().equals(&quot;&quot;))
              {
                  JOptionPane.showMessageDialog(null, &quot;No Selection Was Made&quot;,&quot;Error Message&quot;, JOptionPane.ERROR_MESSAGE);
              }
              else
              {
                  float totalBill = eb + sc;
                  tbTF.setText(String.valueOf(totalBill));
              }
          }
      else if(e.getSource()==clr)
      {
          anTF.setText(&quot;&quot;);
          cnTF.setText(&quot;&quot;);
          prTF.setText(&quot;&quot;);
          crTF.setText(&quot;&quot;);
          kuTF.setText(&quot;&quot;);
          ebTF.setText(&quot;&quot;);
          scTF.setText(&quot;&quot;);
          tbTF.setText(&quot;&quot;);
          df.setState(true);
          ch.select(0);
      }
      }
      catch (ArrayIndexOutOfBoundsException e1) {
      JOptionPane.showMessageDialog(null, &quot;Input a correct positive integer value&quot;, &quot;Error&quot;, JOptionPane.ERROR_MESSAGE);
  }
  }
  }
  </code></pre>