Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Will not average for high temp

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Will not average for high temp

    This program will not give me the average high temperature for the week and I cannot figure out why! Every time it only spits out "0" whereas the average low temperature works great... Please help!

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class Temps_Grid_Keenan extends JFrame
    {
      private static final long serialVersionUID = 1L;
      private static final int WIDTH = 400;
      private static final int HEIGHT = 500;
     
      private JTextField high1Field, low1Field, high2Field, low2Field;
      private JTextField high3Field, low3Field, high4Field, low4Field;
      private JTextField high5Field, low5Field, high6Field, low6Field;
      private JTextField high7Field, low7Field, ave_highField, ave_lowField;
     
      String temperatureText, aveHigh1 = "0", aveLow1 = "0";
      public int count = 1, count2 = 1;
      public int high1Temp, high2Temp, high3Temp, high4Temp;
      public int high5Temp, high6Temp, high7Temp, totalHigh = 0;
      public int low1Temp, low2Temp, low3Temp, low4Temp;
      public int low5Temp, low6Temp, low7Temp, totalLow = 0;
      public int aveHigh = 0, aveLow = 0, total = 0;
      JLabel weekdayLabel, highLabel, lowLabel;
      JLabel tempLabel;
     
      public Temps_Grid_Keenan() 
      {
        setTitle("Weekly Temperature Average");
        setSize(WIDTH, HEIGHT);
        setLayout(new GridLayout(11,3,5,5));
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        createContents();
        setVisible(true);
      }
     
      private void createContents()
      {
        weekdayLabel = new JLabel("Weekday");
        add(weekdayLabel);
        highLabel = new JLabel("High");
        add(highLabel);
        lowLabel = new JLabel("Low");
        add(lowLabel);
     
        JButton sundayButton = new JButton("Sunday");
        sundayButton.addActionListener(new ButtonListener());
        add(sundayButton);
        high1Field = new JTextField(15);
        add(high1Field);
        low1Field = new JTextField(15);
        add(low1Field);
     
        JButton mondayButton = new JButton("Monday");
        mondayButton.addActionListener(new ButtonListener());
        add(mondayButton);
        high2Field = new JTextField(15);
        add(high2Field);
        low2Field = new JTextField(15);
        add(low2Field);
     
        JButton tuesdayButton = new JButton("Tuesday");
        tuesdayButton.addActionListener(new ButtonListener());
        add(tuesdayButton);
        high3Field = new JTextField(15);
        add(high3Field);
        low3Field = new JTextField(15);
        add(low3Field);
     
        JButton wednesdayButton = new JButton("Wednesday");
        wednesdayButton.addActionListener(new ButtonListener());
        add(wednesdayButton);
        high4Field = new JTextField(15);
        add(high4Field);
        low4Field = new JTextField(15);
        add(low4Field);
     
        JButton thursdayButton = new JButton("Thursday");
        thursdayButton.addActionListener(new ButtonListener());
        add(thursdayButton);
        high5Field = new JTextField(15);
        add(high5Field);
        low5Field = new JTextField(15);
        add(low5Field);
     
        JButton fridayButton = new JButton("Friday");
        fridayButton.addActionListener(new ButtonListener());
        add(fridayButton);
        high6Field = new JTextField(15);
        add(high6Field);
        low6Field = new JTextField(15);
        add(low6Field);
     
        JButton saturdayButton = new JButton("Saturday");
        saturdayButton.addActionListener(new ButtonListener());
        add(saturdayButton);
        high7Field = new JTextField(15);
        add(high7Field);
        low7Field = new JTextField(15);
        add(low7Field);
     
        JLabel ave_highLabel = new JLabel("Average High");
        add(ave_highLabel);
        ave_highField = new JTextField(15);
        add(ave_highField);
        JLabel blankLabel = new JLabel();
        add(blankLabel);
        JLabel ave_lowLabel = new JLabel("Average Low");
        add(ave_lowLabel);
        ave_lowField = new JTextField(15);
        add(ave_lowField);
     
        high1Field.addActionListener(new ButtonListener());
        high2Field.addActionListener(new ButtonListener());
        high3Field.addActionListener(new ButtonListener());
        high4Field.addActionListener(new ButtonListener());
        high5Field.addActionListener(new ButtonListener());
        high6Field.addActionListener(new ButtonListener());
        high7Field.addActionListener(new ButtonListener());
     
        low1Field.addActionListener(new ButtonListener());
        low2Field.addActionListener(new ButtonListener());
        low3Field.addActionListener(new ButtonListener());
        low4Field.addActionListener(new ButtonListener());
        low5Field.addActionListener(new ButtonListener());
        low6Field.addActionListener(new ButtonListener());
        low7Field.addActionListener(new ButtonListener());
      }
     
      private class ButtonListener implements ActionListener
      {
        public void actionPerformed(ActionEvent e)
        { 
          if(e.getActionCommand().equals("Sunday"))
          {
            clearFields();
            high1Field.setBackground(Color.YELLOW);
            low1Field.setBackground(Color.YELLOW);
          }
          else if (e.getActionCommand().equals("Monday"))
          {
            clearFields();
            high2Field.setBackground(Color.YELLOW);
            low2Field.setBackground(Color.YELLOW);
          }
          else if (e.getActionCommand().equals("Tuesday"))
          {
            clearFields();
            high3Field.setBackground(Color.YELLOW);
            low3Field.setBackground(Color.YELLOW);
          }
          else if (e.getActionCommand().equals("Wednesday"))
          {
            clearFields();
            high4Field.setBackground(Color.YELLOW);
            low4Field.setBackground(Color.YELLOW);
          }
          else if (e.getActionCommand().equals("Thursday"))
          {
            clearFields();
            high5Field.setBackground(Color.YELLOW);
            low5Field.setBackground(Color.YELLOW);
          }
          else if (e.getActionCommand().equals("Friday"))
          {
            clearFields();
            high6Field.setBackground(Color.YELLOW);
            low6Field.setBackground(Color.YELLOW);
          }
          else if (e.getActionCommand().equals("Saturday"))
          {
            clearFields();
            high7Field.setBackground(Color.YELLOW);
            low7Field.setBackground(Color.YELLOW);
          }
     
    /********************** Input High and Low ***********************/
     
    /************************ Sunday High ****************************/
     
          if (e.getSource() == high1Field)                          
          {          
            high1Temp = Integer.parseInt(high1Field.getText()); 
            totalHigh = totalHigh + high1Temp;
            calcAverage(totalHigh, count);
            count++;
          }
     
    /************************ Sunday Low ****************************/
     
          else if (e.getSource() == low1Field)                      
          {
            low1Temp = Integer.parseInt(low1Field.getText()); 
            totalLow = totalLow + low1Temp; 
            calcAverage1(totalLow, count2);
            count2++;
          }
     
    /************************ Monday High ****************************/
     
          else if (e.getSource() == high2Field)
          {          
            high2Temp = Integer.parseInt(high2Field.getText()); 
            totalHigh = totalHigh + high2Temp; 
            calcAverage(totalHigh, count);
            count++;
          }
     
    /************************ Monday Low ****************************/
     
          else if (e.getSource() == low2Field)                     
          { 
            low2Temp = Integer.parseInt(low2Field.getText()); 
            totalLow = totalLow + low2Temp; 
            calcAverage1(totalLow, count2);;
            count2++;
          }
     
    /************************ Tuesday High ****************************/
     
          else if (e.getSource() == high3Field)
          {          
            high3Temp = Integer.parseInt(high3Field.getText()); 
            totalHigh = totalHigh + high3Temp; 
            calcAverage(totalHigh, count);
            count++;
          }
     
    /************************ Tuesday Low ****************************/
     
          else if (e.getSource() == low3Field)
          {
            low3Temp = Integer.parseInt(low3Field.getText()); 
            totalLow = totalLow + low3Temp; 
            calcAverage1(totalLow, count2);
            count2++;
          }
     
    /************************ Wednesday High ****************************/
     
          else if (e.getSource() == high4Field)
          {          
            high4Temp = Integer.parseInt(high4Field.getText()); 
            totalHigh = totalHigh + high4Temp; 
            calcAverage(totalHigh, count);
            count++;
          }
     
    /************************ Wednesday Low ****************************/
     
          else if (e.getSource() == low4Field)           
          {
            low4Temp = Integer.parseInt(low4Field.getText()); 
            totalLow = totalLow + low4Temp; 
            calcAverage1(totalLow, count2);
            count2++;
          }
     
    /************************ Thursday High ****************************/
     
          else if (e.getSource() == high5Field)
          {          
            high5Temp = Integer.parseInt(high5Field.getText()); 
            totalHigh = totalHigh + high5Temp; 
            calcAverage(totalHigh, count);
            count++;
          }
     
    /************************ Thursday Low ****************************/
     
          else if (e.getSource() == low5Field)
          {
            low5Temp = Integer.parseInt(low5Field.getText()); 
            totalLow = totalLow + low5Temp; 
            calcAverage1(totalLow, count2);
            count2++;
          }
     
    /************************ Friday High ****************************/
     
          else if (e.getSource() == high6Field)
          {          
            high6Temp = Integer.parseInt(high6Field.getText()); 
            totalHigh = totalHigh + high6Temp; 
            calcAverage(totalHigh, count);
            count++;
          }
     
    /************************ Friday Low ****************************/
     
          else if (e.getSource() == low6Field)
          {
            low6Temp = Integer.parseInt(low6Field.getText()); 
            totalLow = totalLow + low6Temp;
            calcAverage1(totalLow, count2);
            count2++;
          }
     
    /************************ Saturday High ****************************/
     
          else if (e.getSource() == high7Field)
          {          
            high7Temp = Integer.parseInt(high7Field.getText()); 
            totalHigh = totalHigh + high7Temp; 
            calcAverage(totalHigh, count);
            count++;
          }
     
    /************************ Saturday Low ****************************/
     
          else if (e.getSource() == low7Field)
          {
            low7Temp = Integer.parseInt(low7Field.getText()); 
            totalLow = totalLow + low7Temp; 
            calcAverage1(totalLow, count2);
            count2++;
          } 
     
          ave_highField.setText(aveHigh1);
          ave_lowField.setText(aveLow1);
        }
      }
     
      /************** Calculate average high and low; clear fields ***************************/
     
      public void calcAverage(int totalHigh, int count)
      {
        aveHigh = totalHigh / count;
        aveHigh1 = Integer.toString(aveHigh);
      }
      public void calcAverage1(int totalLow, int count2)
      {
        aveLow = totalLow / count2;
        aveLow1 = Integer.toString(aveLow);
      }
     
      public void clearFields()
      {
        high1Field.setBackground(Color.WHITE);
        low1Field.setBackground(Color.WHITE);
        high2Field.setBackground(Color.WHITE);
        low2Field.setBackground(Color.WHITE);
        high3Field.setBackground(Color.WHITE);
        low3Field.setBackground(Color.WHITE);
        high4Field.setBackground(Color.WHITE);
        low4Field.setBackground(Color.WHITE);
        high5Field.setBackground(Color.WHITE);
        low5Field.setBackground(Color.WHITE);
        high6Field.setBackground(Color.WHITE);
        low6Field.setBackground(Color.WHITE);
        high7Field.setBackground(Color.WHITE);
        low7Field.setBackground(Color.WHITE);
      }
     
      public static void main (String [] args)
      {
        new Temps_Grid_Keenan();
      }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Will not average for high temp

    Is the code using integer math? 4/6 = 0
    or floating point? 4/6.0 = 0.6666
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Will not average for high temp

    floating point

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Will not average for high temp

    it only spits out "0"
    Can you copy the output and post it here that shows what the code outputs?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jun 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Will not average for high temp


  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Will not average for high temp

    Try debugging the code by adding some println statements to the code where the value is computed. Print out the values of all of the inputs and the values of all the intermediate results so you can see what the code is doing and where it is going wrong.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jun 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Will not average for high temp

    How do i "debug"?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Will not average for high temp

    I use the println method to print values on the console:
     System.out.println("aH1="+aveHigh1); // show variable's value
    Add the above after the line that computes the value.
    Do the same for all the other variables that are used.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Average Rainfall Main Class Using nested for loops,input validation - Average is off
    By CyberOps in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 24th, 2014, 05:36 AM
  2. Print Student average, high, and low grades through array?
    By MLeclerc182 in forum Object Oriented Programming
    Replies: 2
    Last Post: May 10th, 2012, 06:12 AM
  3. Replies: 1
    Last Post: January 12th, 2012, 09:14 AM
  4. GUI temp converter program troubles
    By copelandtml in forum What's Wrong With My Code?
    Replies: 10
    Last Post: August 14th, 2011, 10:28 AM