-6

I am trying to create a program to calculate the percentage of a student's attendance. The program calculates it just fine, but I have a problem with the output. If the student has 100% attendance, the program will display the percentage as intended. But when the student has less than 100%, the program will display the percentage as 0% no matter what value was entered.

The main part of the GUI

class Frame extends JFrame
{
    private JButton calcButton;
    private JTextField daysPresentText;
    private JLabel instructionLabel;
    private JComboBox<String> intakeComboBox;
    private JLabel intakeLabel;
    private JLabel maxDaysLabel;
    private JComboBox<String> semesterComboBox;
    private JLabel semesterLabel;
    private JLabel statusLabel;
    private JLabel studentLabel;
    static JTextField studentText;

    String semester, intake, daysPresent, semesterLength;
    int maxDays, days;
    static double attendancePercentage;

    public Frame() 
    {
       initComponents();


    }

     @SuppressWarnings("unchecked")                         
    private void initComponents() 
    {

        studentText = new JTextField();
        instructionLabel = new JLabel();
        statusLabel = new JLabel();
        semesterLabel = new JLabel();
        studentLabel = new JLabel();
        calcButton = new JButton();
        semesterComboBox = new JComboBox<>();
        daysPresentText = new JTextField();
        maxDaysLabel = new JLabel();
        intakeLabel = new JLabel();
        intakeComboBox = new JComboBox<>();

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        instructionLabel.setText("Enter student name and days present");

        statusLabel.setText("Days Present");

        semesterLabel.setText("Semester");

        studentLabel.setText("Student");

        calcButton.setText("Calculate Percentage");
        calcButton.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent evt) 
            {
                calcButtonActionPerformed(evt);
            }
        });

        semesterComboBox.setModel(new DefaultComboBoxModel<>(new String[] { "1", "2", "3", "4", "5","6","7"}));
        semesterComboBox.addItemListener(new ItemListener() 
        {
            public void itemStateChanged(ItemEvent evt) 
            {
                semesterComboBoxItemStateChanged(evt);
            }
        });

        daysPresentText.setText("");

        intakeLabel.setText("Intake");

        intakeComboBox.setModel(new DefaultComboBoxModel<>(new String[] { "March", "August"}));
        intakeComboBox.addItemListener(new ItemListener() 
        {
            public void itemStateChanged(ItemEvent evt) 
            {
                intakeComboBoxItemStateChanged(evt);
            }
        });

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                    .addComponent(studentLabel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(instructionLabel, GroupLayout.DEFAULT_SIZE, 257, Short.MAX_VALUE)
                    .addComponent(studentText))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(semesterComboBox, GroupLayout.PREFERRED_SIZE, 113, GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(intakeComboBox, GroupLayout.PREFERRED_SIZE, 96, GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(semesterLabel, GroupLayout.PREFERRED_SIZE, 113, GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(intakeLabel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(daysPresentText, GroupLayout.PREFERRED_SIZE, 95, GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(maxDaysLabel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addComponent(statusLabel, GroupLayout.PREFERRED_SIZE, 184, GroupLayout.PREFERRED_SIZE)))
                    .addComponent(calcButton, GroupLayout.PREFERRED_SIZE, 198, GroupLayout.PREFERRED_SIZE))
                .addGap(30, 30, 30))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(instructionLabel, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE)
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(statusLabel, GroupLayout.PREFERRED_SIZE, 58, GroupLayout.PREFERRED_SIZE)
                            .addComponent(intakeLabel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                .addComponent(intakeComboBox, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE)
                                .addComponent(daysPresentText, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE))
                            .addComponent(studentText)
                            .addComponent(semesterComboBox)
                            .addComponent(maxDaysLabel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(studentLabel, GroupLayout.PREFERRED_SIZE, 58, GroupLayout.PREFERRED_SIZE)
                        .addComponent(semesterLabel, GroupLayout.PREFERRED_SIZE, 58, GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addComponent(calcButton, GroupLayout.PREFERRED_SIZE, 63, GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }     

    private void semesterComboBoxItemStateChanged(ItemEvent evt) 
    {     
     semester=evt.getItem().toString();
    }                                                 

    private void intakeComboBoxItemStateChanged(ItemEvent evt) 
    {                                                
     intake=evt.getItem().toString();
    }

    private void calcButtonActionPerformed(ActionEvent evt)
    {
       daysPresent=daysPresentText.getText();       
       days=Integer.valueOf(daysPresent);
        maxDays=98;
        if(days>maxDays)
        {
          errorMessage EM=new errorMessage();
          EM.setVisible(true);
        }else
        {
        calculate();
        CalculateDisplay CD=new CalculateDisplay();
        CD.setVisible(true);
        }
    }                      


    public void calculate()
    {
        attendancePercentage=(days/maxDays)*100;
    }
}

The display for the output of the equation

   class CalculateDisplay extends JFrame 
{
    private JLabel displayLabel;
    private JButton okButton;

    String student;
    double attendancePercentage;

    public CalculateDisplay() 
    {
        initComponents();
        student=Frame.studentText.getText();
        attendancePercentage=Frame.attendancePercentage;
        displayLabel.setText("The attendance percentage of "+student+" is 
        "+attendancePercentage+".");
    }
  • 7
    Even without reading the ridiculous amount of code you provided, this sounds like an integer division problem, make sure one of the numbers provided in your percentage calculation is a floating point/decimal number type – Nick Jun 13 '18 at 12:23
  • 2
    Too... much... code... Narrow things down to their essence, which is a couple lines including calculate, which doors integer division. Simple debugging or logging would show you this. – Dave Newton Jun 13 '18 at 12:26
  • 2
    have you debugged your code? seeing as most of the code you posted is Netbeans generated Swing code, I'll just assume you're using an IDE which contains a debugger – Stultuske Jun 13 '18 at 12:26
  • 2
    Possible duplicate of [Why is the result of 1/3 == 0?](https://stackoverflow.com/questions/4685450/why-is-the-result-of-1-3-0) – Nick Jun 13 '18 at 12:27
  • 1
    `attendancePercentage=(days/maxDays)*100;` with `int maxDays, days;` @NickA is right – jhamon Jun 13 '18 at 12:29
  • @jhamon Yeah, was on my phone for the first comment, checked for the `int` definitions when returned to PC before dupe CV – Nick Jun 13 '18 at 12:31

1 Answers1

0

Its a basic integer divison problem Integer division: How do you produce a double?

Either change int maxDays, days; to double maxDays, days; or change your calculate method from:

attendancePercentage=(days/maxDays)*100;

to:

attendancePercentage = ((double) days / (double) maxDays) * 100;`
double-beep
  • 3,889
  • 12
  • 24
  • 35
CoderNeji
  • 1,948
  • 3
  • 17
  • 29