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 9 of 9

Thread: Adding if statements.

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Adding if statements.

    Few errors: in bold: cannot find symbol ( for the things in bold )
    trying to add together the if statements, but it isn't working, Im confused. My question is, how do i add what my if statements return?

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    /**
     *
     * @author Logan
     */
    public class MidtermGUI extends javax.swing.JFrame {
     
        /**
         * Creates new form MidtermGUI
         */
        public MidtermGUI() {
            initComponents();
        }
     
        /**
         * This method is called from within the constructor to initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is always
         * regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
     
            buttonGroup1 = new javax.swing.ButtonGroup();
            Printer = new javax.swing.JCheckBox();
            Speakers = new javax.swing.JCheckBox();
            Mouse = new javax.swing.JCheckBox();
            Mousepad = new javax.swing.JCheckBox();
            Headset = new javax.swing.JCheckBox();
            Thumbdrive = new javax.swing.JCheckBox();
            SelectPeriphMsg = new javax.swing.JLabel();
            SelectDsrdShipping = new javax.swing.JLabel();
            CostWShipping = new javax.swing.JButton();
            CostWOutShipping = new javax.swing.JButton();
            Output = new javax.swing.JTextField();
            Ground = new javax.swing.JRadioButton();
            Air = new javax.swing.JRadioButton();
            FedEx = new javax.swing.JRadioButton();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
     
            Printer.setText("Printer");
            Printer.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    PrinterActionPerformed(evt);
                }
            });
     
            Speakers.setText("Speakers");
     
            Mouse.setText("Mouse");
     
            Mousepad.setText("Mousepad");
     
            Headset.setText("Headset");
     
            Thumbdrive.setText("Thumb Drive");
     
            SelectPeriphMsg.setText("Please Select Wanted Peripherals");
     
            SelectDsrdShipping.setText("Please Select Desired Shipping Courier");
     
            CostWShipping.setText("Cost w/ Shipping");
            CostWShipping.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    CostWShippingActionPerformed(evt);
                }
            });
     
            CostWOutShipping.setText("Cost w/out Shipping");
            CostWOutShipping.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    CostWOutShippingActionPerformed(evt);
                }
            });
     
            buttonGroup1.add(Ground);
            Ground.setText("Ground");
     
            buttonGroup1.add(Air);
            Air.setText("Air");
     
            buttonGroup1.add(FedEx);
            FedEx.setText("FedEx");
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(SelectDsrdShipping)
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(CostWShipping)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(CostWOutShipping)
                            .addGap(48, 48, 48))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addGap(0, 0, Short.MAX_VALUE)
                            .addComponent(SelectPeriphMsg)
                            .addGap(18, 18, 18)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                    .addComponent(Printer)
                                    .addGap(29, 29, 29))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(Mousepad)
                                    .addGap(11, 11, 11)))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(Headset)
                                .addComponent(Speakers))
                            .addGap(18, 18, 18)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(Mouse)
                                .addComponent(Thumbdrive))
                            .addGap(62, 62, 62))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(10, 10, 10)
                            .addComponent(Output)
                            .addContainerGap())
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(FedEx)
                                .addComponent(Air)
                                .addComponent(Ground))
                            .addGap(0, 0, Short.MAX_VALUE))))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(Printer)
                        .addComponent(Speakers)
                        .addComponent(Mouse)
                        .addComponent(SelectPeriphMsg))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(Mousepad)
                        .addComponent(Headset)
                        .addComponent(Thumbdrive))
                    .addGap(44, 44, 44)
                    .addComponent(SelectDsrdShipping)
                    .addGap(18, 18, 18)
                    .addComponent(Ground)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(Air)
                    .addGap(3, 3, 3)
                    .addComponent(FedEx)
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(CostWShipping)
                        .addComponent(CostWOutShipping))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(Output, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
     
            pack();
        }// </editor-fold>
     
     
     
     
     
        private void CostWShippingActionPerformed(java.awt.event.ActionEvent evt) {
     
        }
     
        private void CostWOutShippingActionPerformed(java.awt.event.ActionEvent evt) {
     
        }
     
        private void PrinterActionPerformed(java.awt.event.ActionEvent evt) {
            // TODO add your handling code here:
        }
        public double calculateCost () {
            //one unit
           double theCost = 1;
           double finalCost = 1;
           double computerBase = 500;
           double Printer = 35;
           double Speakers = 75;
           double mouse = 34;
           double mousepad = 15;
           double headset = 35;
           double thumbdrive = 12;
           return finalCost;
     
        }
        public double calculateCost (int computerAmounts, boolean shipping) {
     
           double computerBase = 500;
     
     
           //If statements for 1 Computer
           //Calculations below.
           {
           if (Printer.isSelected()) {
               double printer = 35;
           }
     
           if (Speakers.isSelected()) {
               double speakers = 75;
     
           }
           if (Mouse.isSelected()) {
               double mouse = 34;
           }
           if (Mousepad.isSelected()){
               double mousepad = 15;
           }
           if (Headset.isSelected()) {
               double headset = 35;
           }
     
           if (Thumbdrive.isSelected()) {
               double thumbdrive = 12;
           }
           double finalCostOfComputer = computerBase+[B]printer+speakers+mouse+mousepad+headset;[/B]
     
           //Computing cost of 1 computer.
     
     
           return finalCostOfComputer;
        }
        }
        public double getDiscountPercantage(int quantity, double discount) {
            discount = .01;
            return discount;
        }
     
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            /* 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 [url=http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html]How to Set the Look and Feel (The Java™ Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel)[/url] 
             */
            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(MidtermGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(MidtermGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(MidtermGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(MidtermGUI.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 MidtermGUI().setVisible(true);
                }
            });
        }
     
        // Variables declaration - do not modify
        private javax.swing.JRadioButton Air;
        private javax.swing.JButton CostWOutShipping;
        private javax.swing.JButton CostWShipping;
        private javax.swing.JRadioButton FedEx;
        private javax.swing.JRadioButton Ground;
        private javax.swing.JCheckBox Headset;
        private javax.swing.JCheckBox Mouse;
        private javax.swing.JCheckBox Mousepad;
        private javax.swing.JTextField Output;
        private javax.swing.JCheckBox Printer;
        private javax.swing.JLabel SelectDsrdShipping;
        private javax.swing.JLabel SelectPeriphMsg;
        private javax.swing.JCheckBox Speakers;
        private javax.swing.JCheckBox Thumbdrive;
        private javax.swing.ButtonGroup buttonGroup1;
        // End of variables declaration
    }
    Last edited by jps; October 25th, 2012 at 11:42 PM.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Adding if statements.

    Please see the announcements page for the use of code tags, and then fix the posted code so that it is indented properly. That way someone will read it.
    What is your question?

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Adding if statements.

    Fixed. I am trying to add up the returns of all of my if statements, but where I have tried to do it, they all say
    double finalCostOfComputer = computerBase+printer+speakers+mouse+mousepad+heads et
    each of the peripherals, printer, etc all say Cannot Find Symbol, even though they are declared above.

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Adding if statements.

    I am trying to add up the returns of all of my if statements
    copy paste what you want to add, and copy paste what you want it added to so it is clear what you want.



    even though they are declared above.
    This is part of the problem.

  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Adding if statements.

    I am trying to add up the returns of all of my if statements
    copy paste what you want to add, and copy paste what you want it added to so it is clear what you want.



    even though they are declared above.
    This is part of the problem.

  6. #6
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Adding if statements.

    So instead of declaring them in the if statements, I should just return them?

  7. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Adding if statements.

    public double calculateCost () {
    //one unit
    double theCost = 1;
    double finalCost = 1;
    double computerBase = 500;
    double Printer = 35;
    double Speakers = 75;
    double mouse = 34;
    double mousepad = 15;
    double headset = 35;
    double thumbdrive = 12;
    return finalCost;

    }


    public double calculateCost (int computerAmounts, boolean shipping) {

    double computerBase = 500;


    //If statements for 1 Computer
    //Calculations below.
    {
    if (Printer.isSelected()) {
    double printer = 35;
    }

    if (Speakers.isSelected()) {
    double speakers = 75;

    }
    if (Mouse.isSelected()) {
    double mouse = 34;
    }
    if (Mousepad.isSelected()){
    double mousepad = 15;
    }
    if (Headset.isSelected()) {
    double headset = 35;
    }

    if (Thumbdrive.isSelected()) {
    double thumbdrive = 12;
    }
    double finalCostOfComputer = computerBase+printer+speakers+mouse+mousepad+headset;

    //Computing cost of 1 computer.


    return finalCostOfComputer;
    }
    Your code has the same method name twice. Inside both methods several variables are declared and probably shouldn't have been (going on my assumptions on what you want the code to do). The next part is you said you wanted to add them together. Did you mean combine the two methods into one or add the elements inside the method to a total? That is the part I do not understand, what you want to add together.

    When you set the value of
    finalCostOfComputer
    equal to
    computerBase+printer+speakers+mouse+mousepad+heads et;
    these variables point to the class variables, which have nothing to do with the variables declared in the same method. Each of those variables go out of scope at the end of their respective code block, after doing nothing.

  8. #8
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Adding if statements.

    Sorry for the confusion, I deleted the duplicate method and I am trying to add up the sum of all the elements in the method to a total. What should I try next?

  9. #9
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Adding if statements.

    You should reread everything I have posted and research the term "scope" in terms of programming. Then fix the re-declaring of variables-with-the-same-name and start setting values to the variables being added together to be returned.

Similar Threads

  1. if else statements
    By mozyman in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 24th, 2010, 08:06 PM
  2. If statements
    By Scottj996 in forum Java Theory & Questions
    Replies: 1
    Last Post: August 16th, 2010, 10:09 AM
  3. not sure what im doing wrong with these if else statements
    By yrvd86 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 26th, 2010, 08:32 PM
  4. continue statements
    By monroe in forum Java Applets
    Replies: 1
    Last Post: March 20th, 2010, 06:26 PM
  5. Need help with While and For Statements
    By duckman in forum Loops & Control Statements
    Replies: 2
    Last Post: October 20th, 2009, 08:42 PM