Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Why Isn't my Code Working?

+8 votes
asked Mar 23, 2025 by Zenith Sccorno (280 points)
Hello, I'm currently taking a Java course and I'm not sure what's wrong with my code. I had to cut a lot of it out but this is the gist of it (it's a GUI):

    public static void main(String args[]) {

      

                double coursemark1;

                double coursemark2;

                double coursemark3;

                double coursemark4;

                char percent;

                double result;

               

                percent = '%';

               

                coursemark1 = Double.parseDouble(txtmarkinput1.getText());

                coursemark2 = Double.parseDouble(txtmarkinput2.getText());

                coursemark3 = Double.parseDouble(txtmarkinput3.getText());

                coursemark4 = Double.parseDouble(txtmarkinput4.getText());

               

                result = percent + (coursemark1 + coursemark2 + coursemark3 + coursemark4) / 4;

                result = result * 100;

                result = Math.round (result);

                result = result/100;

               

                lblAnswer.setText(String.vaueOf(result));

        /* Set the Nimbus look and feel */

        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">

        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.

         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html

         */

        try {

            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {

                if ("Nimbus".equals(info.getName())) {

                    javax.swing.UIManager.setLookAndFeel(info.getClassName());

                    break;

                }

            }

        } catch (ClassNotFoundException ex) {

            java.util.logging.Logger.getLogger(averageproject.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

        } catch (InstantiationException ex) {

            java.util.logging.Logger.getLogger(averageproject.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

        } catch (IllegalAccessException ex) {

            java.util.logging.Logger.getLogger(averageproject.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

        } catch (javax.swing.UnsupportedLookAndFeelException ex) {

            java.util.logging.Logger.getLogger(averageproject.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

        }

        //</editor-fold>

        /* Create and display the form */

        java.awt.EventQueue.invokeLater(new Runnable() {

           public void run() {

               

                new averageproject().setVisible(true);

            }

        });

    }

    // Variables declaration - do not modify                    

    private javax.swing.JButton buxcalulate;

    private javax.swing.JLabel jLabel1;

    private javax.swing.JLabel jLabel2;

    private javax.swing.JLabel jLabel7;

    private javax.swing.JPanel jPanel1;

    private javax.swing.JLabel lblAnswer;

    private javax.swing.JLabel lblcoursemark1;

    private javax.swing.JLabel lblcoursemark2;

    private javax.swing.JLabel lblcoursemark3;

    private javax.swing.JLabel lblcoursemark4;

    private javax.swing.JTextField txtmarkinput1;

    private javax.swing.JTextField txtmarkinput2;

    private javax.swing.JTextField txtmarkinput3;

    private javax.swing.JTextField txtmarkinput4;

    // End of variables declaration                  

}

1 Answer

+3 votes
answered Jun 29, 2025 by Jerry Jeremiah (2,040 points)
You don't have a line that declares your class, but it should be called averageproject - it needs to extend JFrame so that you can add controls to it.  And then you need to add the controls to it.

The button also needs an actionListener to execute the stuff that will run when you push the button but Main is not an action listener.

And main is static so that means you can't use class variables inside Main.  You could make them variables in Main and that would get rid of that error but then the JFrame can't access them.  So, what you really need to do is to put everything in Main (except java.awt.EventQueue.invokeLater) into a normal constructor.

And anyway it won't on OnlineGDB because currently GUI is supported for python language only.  So you can ONLY write console programs (except python)
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...