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

Thread: Few errors. Returning.

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

    Default Few errors. Returning.

    Program is saying im missing return statements in the methods, areaCircle, areaTriangle, and areaRectangle even though they are there. What should I do?


    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package swingarea;
     
    /**
     *
     * @author Logan
     */
    public class AreaCalculator extends javax.swing.JFrame {
     
        public AreaCalculator() {
            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();
            dimension1Label = new javax.swing.JLabel();
            rectangleRadioButton = new javax.swing.JRadioButton();
            triangleRadioButton = new javax.swing.JRadioButton();
            dimension2Label = new javax.swing.JLabel();
            dimension1Text = new javax.swing.JTextField();
            dimension2Text = new javax.swing.JTextField();
            inchesLabel1 = new javax.swing.JLabel();
            inchesLabel2 = new javax.swing.JLabel();
            calcAreaButton = new javax.swing.JButton();
            areaLabel = new javax.swing.JLabel();
            outputLabel = new javax.swing.JLabel();
            circleRadioButton = new javax.swing.JRadioButton();
            jMenuBar1 = new javax.swing.JMenuBar();
            jMenu1 = new javax.swing.JMenu();
            clearMenuItem = new javax.swing.JMenu();
            exitMenuItem = new javax.swing.JMenu();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("Area Calculator");
     
            dimension1Label.setText("Dimension 1:");
     
            buttonGroup1.add(rectangleRadioButton);
            rectangleRadioButton.setText("Rectangle");
     
            buttonGroup1.add(triangleRadioButton);
            triangleRadioButton.setText("Triangle");
     
            dimension2Label.setText("Dimension 2:");
     
            inchesLabel1.setText("inches");
     
            inchesLabel2.setText("inches");
     
            calcAreaButton.setText("Calculate Area");
            calcAreaButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    calcAreaButtonActionPerformed(evt);
                }
            });
     
            areaLabel.setText("Area is:");
     
            buttonGroup1.add(circleRadioButton);
            circleRadioButton.setText("Circle");
     
            jMenu1.setText("File");
            jMenuBar1.add(jMenu1);
     
            clearMenuItem.setText("Clear");
            clearMenuItem.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    clearMenuItemActionPerformed(evt);
                }
            });
            jMenuBar1.add(clearMenuItem);
     
            exitMenuItem.setText("Exit");
            exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    exitMenuItemActionPerformed(evt);
                }
            });
            jMenuBar1.add(exitMenuItem);
     
            setJMenuBar(jMenuBar1);
     
            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(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(circleRadioButton)
                                    .addComponent(dimension1Label))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(dimension1Text, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(inchesLabel1))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(rectangleRadioButton)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(triangleRadioButton))))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(10, 10, 10)
                                        .addComponent(areaLabel))
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(calcAreaButton)
                                        .addComponent(outputLabel)))
                                .addGap(48, 48, 48)))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(dimension2Label)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(dimension2Text, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(inchesLabel2))))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(circleRadioButton)
                        .addComponent(rectangleRadioButton)
                        .addComponent(triangleRadioButton))
                    .addGap(41, 41, 41)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(dimension1Label)
                        .addComponent(dimension1Text, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(inchesLabel1))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(dimension2Label)
                        .addComponent(dimension2Text, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(inchesLabel2))
                    .addGap(31, 31, 31)
                    .addComponent(calcAreaButton)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(areaLabel)
                    .addGap(20, 20, 20)
                    .addComponent(outputLabel))
            );
     
            pack();
        }// </editor-fold>
     
        private void calcAreaButtonActionPerformed(java.awt.event.ActionEvent evt) {
     double area = 0; 
     double areaCircle = 0;
            if (circleRadioButton.isSelected()) 
            { 
                String strRadius = dimension1Text.getText(); 
                int radius = Integer.parseInt(strRadius); 
                area = areaCircle; 
            } 
            String strArea = String.valueOf(area); 
            outputLabel.setText(strArea + " square inches");        // TODO add your handling code here:
        }
    public static double areaTriangle(double triBase,double triHeight, double triOutput, int shapeNum) {
            triOutput = (triBase*triHeight)/2;
              if (shapeNum == 1 ){
             return triOutput;
                  }
                }
    public static double areaCircle (double circOutput,double circRadi, int shapeNum )
    {
        {
           circOutput = (Math.pow (circRadi, 2)*Math.PI);    
                 if(shapeNum == 2 )
            {
            return circOutput;
            }
        }
           }
    public static double areaRectangle(int rectOutput, int lengthRect, int widthRect, int shapeNum) {
            rectOutput = lengthRect*widthRect;
              if(shapeNum == 3 ) {
            return rectOutput;
      }  
         }
        private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
            System.exit(0);
        }
     
        private void clearMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
            // TODO add your handling code here:
        }
     
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
             double area = 0; 
     
            //<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(AreaCalculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(AreaCalculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(AreaCalculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(AreaCalculator.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 AreaCalculator().setVisible(true);
                }
            });
        }
         double area = 0; 
     
        // Variables declaration - do not modify
        private javax.swing.JLabel areaLabel;
        private javax.swing.ButtonGroup buttonGroup1;
        private javax.swing.JButton calcAreaButton;
        private javax.swing.JRadioButton circleRadioButton;
        private javax.swing.JMenu clearMenuItem;
        private javax.swing.JLabel dimension1Label;
        private javax.swing.JTextField dimension1Text;
        private javax.swing.JLabel dimension2Label;
        private javax.swing.JTextField dimension2Text;
        private javax.swing.JMenu exitMenuItem;
        private javax.swing.JLabel inchesLabel1;
        private javax.swing.JLabel inchesLabel2;
        private javax.swing.JMenu jMenu1;
        private javax.swing.JMenuBar jMenuBar1;
        private javax.swing.JLabel outputLabel;
        private javax.swing.JRadioButton rectangleRadioButton;
        private javax.swing.JRadioButton triangleRadioButton;
        // End of variables declaration
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Few errors. Returning.

    What will your areaCircle method return if shapeNum != 2?
    Improving the world one idiot at a time!

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

    Default Re: Few errors. Returning.

    ublic static double areaCircle (double circOutput,double circRadi, int shapeNum )
    {
    {
    circOutput = (Math.pow (circRadi, 2)*Math.PI);
    if(shapeNum == 2 ) {
    return circOutput;
    }

    {

    }
    }
    }

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Few errors. Returning.

    Congratulations, you know how to use bold. But it does not answer my question. The code you bolded is what is returned when shapeNum EQUALS 2. I asked what will your code return when shapeNum DOES NOT EQUAL 2?
    Improving the world one idiot at a time!

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

    Default Re: Few errors. Returning.

    What should I set it to return to If I dont want it to do anything?

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Few errors. Returning.

    If you want a method to return a value sometimes and not return a value sometimes then there is a possibility that you have a design flaw. On the other hand this might make sense. Take a look at the indexOf method in the String class for inspiration.
    Improving the world one idiot at a time!

Similar Threads

  1. Replies: 3
    Last Post: March 6th, 2012, 02:50 AM
  2. Method returning 0 for everything
    By JJTierney in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 4th, 2010, 07:51 PM
  3. If statement not returning anything
    By Jonathan_C in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 15th, 2010, 02:38 PM
  4. Returning Null
    By Woody619 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: July 22nd, 2010, 12:53 PM
  5. returning a 2D array
    By straw in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 11th, 2010, 03:30 AM