Notice: Undefined offset: 16343563 in /var/www/html/qa-external/qa-external-users.php on line 744
I Need Help with this Code - OnlineGDB Q&A
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.

I Need Help with this Code

+6 votes
asked Mar 23, 2025 by Zenith Sccorno (280 points)
Hello, I need help with this code. I had to cut a lot of it out due to max character's I'm allowed to used but this is the gist of it (it's a GUI):

        java.awt.EventQueue.invokeLater(new Runnable() {
            public static void main(String args[]) {
        double nut1;
        double nut2;
        double nut3;
        double nut4;
        double nut5;
        double taxes;
        double subtotal;
        double grandtotal;
        char money;
        
        money = '$';
        
        nut1 = money + 2.50 * Double.parseDouble(txtnuts1.getText());
        nut2 = money + 1.50 * Double.parseDouble(txtnuts1.getText());
        nut3 = money + 3.00 * Double.parseDouble(txtnuts3.getText());
        nut4 = money + 1.50 * Double.parseDouble(txtnuts1.getText());
        nut5 = money + 1.50 * Double.parseDouble(txtnuts1.getText());
        
        subtotal = money + nut1 + nut2 + nut3 + nut4+ nut5;
        subtotal = subtotal *100;
        subtotal = Math.round (subtotal);
        subtotal = subtotal/100;
        
        lbltxttotal7.setText(String.valueOf(txttotal7));
        
        taxes = money + subtotal * 0.13;
        
        taxes = taxes *100;
        taxes = Math.round (taxes);
        taxes = taxes/100;
        
        lbltxtTotal6.setText(String.valueOf(txtTotal6));
        
        grandtotal = money + subtotal + taxes;
        grandtotal = grandtotal *100;
        grandtotal = Math.round (grandtotal);
        grandtotal = grandtotal/100;
        
        lbltxtTotal8.setText(String.valueOf(txtTotal8));

1 Answer

0 votes
answered Jun 28, 2025 by Ramakant (170 points)
java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
        try {
            
            double qty1 = Double.parseDouble(txtnuts1.getText());
            double qty2 = Double.parseDouble(txtnuts2.getText());
            double qty3 = Double.parseDouble(txtnuts3.getText());
            double qty4 = Double.parseDouble(txtnuts4.getText());
            double qty5 = Double.parseDouble(txtnuts5.getText());

           
            double nut1 = 2.50 * qty1;
            double nut2 = 1.50 * qty2;
            double nut3 = 3.00 * qty3;
            double nut4 = 1.50 * qty4;
            double nut5 = 1.50 * qty5;

            
            double subtotal = nut1 + nut2 + nut3 + nut4 + nut5;
            subtotal = Math.round(subtotal * 100.0) / 100.0;

            lbltxtTotal7.setText("$" + String.format("%.2f", subtotal));

            
            double taxes = subtotal * 0.13;
            taxes = Math.round(taxes * 100.0) / 100.0;

            lbltxtTotal6.setText("$" + String.format("%.2f", taxes));

            
            double grandtotal = subtotal + taxes;
            grandtotal = Math.round(grandtotal * 100.0) / 100.0;

            lbltxtTotal8.setText("$" + String.format("%.2f", grandtotal));

        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "Please enter valid numeric quantities.");
        }
    }
});

// i think this is right code
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.
...