title: Use One TextField to Input multiple data in Java
body: <p>Im trying to make a program to calculate my average. But i only want to use one TextField for input. Any suggestions?</p>
  
  <p>I tried to make it so when the button changes, it assigns the number to the variable. but it doesnt seem to work.</p>
  
  <p>Also, Java made me assign the variables to 0...i don't know why... but whatever i asign to the variables manually is what is used to calculate the final grade.</p>
  
  <p>this is what i have so far...</p>
  
  <pre><code>public class FinalApplet extends Applet implements ActionListener
  {
      private TextField gradeField, finalField;
      private Button enter;
  
      public void init()
      {
          gradeField = new TextField(8);
          gradeField.addActionListener(this);
          add(gradeField);
  
          enter = new Button("Enter MP1 Grade");
          enter.addActionListener(this);
          add(enter);
  
          finalField = new TextField(8);
          finalField.addActionListener(this);
          add(finalField);
      }
  
      public void actionPerformed(ActionEvent e)
      {
          double mp1 = 0;
          double mp2 = 0;
          double exam1 = 0;
          double mp3 = 0;
          double mp4 = 0;
          double exam2 = 0;
          double finalGrade = 0;
          char letterGrade;
          //if (e.getSource() == gradeField || e.getSource() == enter)
  
              if (enter.getLabel() == "Enter MP1 Grade")
              {
                  mp1 = Double.parseDouble(gradeField.getText());
                  enter.setLabel("Enter MP2 Grade");
              }
              else if (enter.getLabel() == "Enter MP2 Grade")
              {
                  mp2 = Double.parseDouble(gradeField.getText());
                  enter.setLabel("Enter Exam1 Grade");
              }
              else if (enter.getLabel() == "Enter Exam1 Grade")
              {
                  exam1 = Double.parseDouble(gradeField.getText());
                  enter.setLabel("Enter MP3 Grade");
              }
              else if (enter.getLabel() == "Enter MP3 Grade")
              {
                  mp3 = Double.parseDouble(gradeField.getText());
                  enter.setLabel("Enter MP4 Grade");
              }
              else if (enter.getLabel() == "Enter MP4 Grade")
              {
                  mp4 = Double.parseDouble(gradeField.getText());
                  enter.setLabel("Enter Exam2 Grade");
              }
              else if (enter.getLabel() == "Enter Exam2 Grade")
              {
                  exam2 = Double.parseDouble(gradeField.getText());
                  enter.setLabel("Calculate Final Grade");
              }
              else if (enter.getLabel() == "Calculate Final Grade")
              {
                  finalGrade = Math1.calcAverage(mp1, mp2, exam1, mp3, mp4, exam2);
                  letterGrade = Math1.getLetter(finalGrade);
                  finalField.setText(letterGrade + " - " + finalGrade);
                  enter.setLabel("Enter MP1 Grade");
              }
  
      }
  }
  
  public class Math1
  {       
      public static double calcAverage(double mp1, double mp2, double exam1, double mp3, double mp4, double exam2)
      {
          double finalGrade;
  
          finalGrade = (mp1 * .2) + (mp2 * .2) + (exam1 * .1) + (mp3 * .2) + (mp4 * .2) + (exam2 * .1);
  
          return finalGrade;
      }
  
      public static char getLetter(double finalGrade)
      {
          if (finalGrade &gt;= 89.5)
              return 'A';
          else if (finalGrade &gt;= 79.5 &amp;&amp; finalGrade &lt; 89.5)
              return 'B';
          else if (finalGrade &gt;= 69.5 &amp;&amp; finalGrade &lt; 79.5)
              return 'C';
          else if (finalGrade &gt;= 59.5 &amp;&amp; finalGrade &lt; 69.5)
              return 'D';
          else
              return 'F';
      }    
  }
  </code></pre>