Results 1 to 3 of 3

Thread: repetition

  1. #1
    javazers is offline Member
    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0

    Default repetition

    hello there everyone :) im practicing java on jframes using netbeans,i was trying to make a program
    that adds up how many adult/minor male and adult/minor female are after inputting their age,at some point
    i cant seem to figure out how to repeat the whole structure because when i enter a age number that is suppose
    to add another value,it doesnt sum up...for example i already inputted for adult age and that makes it 1,after i input another for
    adult age,..it doesnt display as 2,it still stays 1,.i tried incrementing but i cant get it to work


    and by the way,i cant seem to attach files :(

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /**
     *
     * @author wardrive
     */
    public class frmMain extends javax.swing.JFrame {
        int mMale;
        int aMale;
        int mFemale;
        int aFemale;
        
       
    
        /**
         * Creates new form frmMain
         */
        public frmMain() {
            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();
            jPanel3 = new javax.swing.JPanel();
            jPanel1 = new javax.swing.JPanel();
            male = new javax.swing.JRadioButton();
            female = new javax.swing.JRadioButton();
            jPanel2 = new javax.swing.JPanel();
            jLabel1 = new javax.swing.JLabel();
            jLabel2 = new javax.swing.JLabel();
            jLabel3 = new javax.swing.JLabel();
            jLabel4 = new javax.swing.JLabel();
            minorMale = new javax.swing.JLabel();
            adultMale = new javax.swing.JLabel();
            minorFemale = new javax.swing.JLabel();
            adultFemale = new javax.swing.JLabel();
            add = new javax.swing.JButton();
            getAge = new javax.swing.JTextField();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Gender"));
            jPanel1.setName("");
    
            buttonGroup1.add(male);
            male.setText("Male");
    
            buttonGroup1.add(female);
            female.setText("Female");
    
            javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
            jPanel1.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(34, 34, 34)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(female)
                        .addComponent(male))
                    .addContainerGap(69, Short.MAX_VALUE))
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(male)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(female)
                    .addGap(15, 15, 15))
            );
    
            jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Maturity"));
    
            jLabel1.setText("Minor male:");
    
            jLabel2.setText("Adult male:");
    
            jLabel3.setText("Minor Female:");
    
            jLabel4.setText("Adult Female:");
    
            javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
            jPanel2.setLayout(jPanel2Layout);
            jPanel2Layout.setHorizontalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                            .addComponent(jLabel4)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(adultFemale))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                            .addComponent(jLabel3)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(minorFemale))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                            .addComponent(jLabel2)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(adultMale))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(minorMale)))
                    .addGap(30, 30, 30))
            );
            jPanel2Layout.setVerticalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addGap(14, 14, 14)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(minorMale))
                    .addGap(18, 18, 18)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(adultMale))
                    .addGap(18, 18, 18)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel3)
                        .addComponent(minorFemale))
                    .addGap(18, 18, 18)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel4)
                        .addComponent(adultFemale))
                    .addContainerGap(48, Short.MAX_VALUE))
            );
    
            add.setText("add");
            add.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    addActionPerformed(evt);
                }
            });
    
            javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
            jPanel3.setLayout(jPanel3Layout);
            jPanel3Layout.setHorizontalGroup(
                jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup()
                    .addGap(41, 41, 41)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel3Layout.createSequentialGroup()
                            .addGap(10, 10, 10)
                            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel3Layout.createSequentialGroup()
                                    .addGap(35, 35, 35)
                                    .addComponent(getAge, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(jPanel3Layout.createSequentialGroup()
                                    .addGap(61, 61, 61)
                                    .addComponent(add))
                                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(44, Short.MAX_VALUE))
            );
            jPanel3Layout.setVerticalGroup(
                jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup()
                    .addGap(20, 20, 20)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(getAge, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(29, 29, 29)
                    .addComponent(add)
                    .addGap(33, 33, 33)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(80, 80, 80)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(100, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(35, 35, 35)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGap(22, 22, 22))
            );
    
            pack();
        }// </editor-fold>
    
        private void addActionPerformed(java.awt.event.ActionEvent evt) {                                    
            for(int a=0; a<50; a++){
            int agee=Integer.parseInt(getAge.getText());
            
            if(male.isSelected()){
                
                if(agee>18){
                   
                     minorMale.setText("" + 1);
                     
                 }
                 else{
                     adultMale.setText("" +1);
                 }
             }
            else{
                if(agee>18){
                    minorFemale.setText("" +1);
                }
                else{
                    adultFemale.setText("" +1);
                }
            }}
        }                                   
    
        /**
         * @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
             * 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(frmMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(frmMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(frmMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(frmMain.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 frmMain().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify
        private javax.swing.JButton add;
        private javax.swing.JLabel adultFemale;
        private javax.swing.JLabel adultMale;
        private javax.swing.ButtonGroup buttonGroup1;
        private javax.swing.JRadioButton female;
        private javax.swing.JTextField getAge;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JLabel jLabel4;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JPanel jPanel2;
        private javax.swing.JPanel jPanel3;
        private javax.swing.JRadioButton male;
        private javax.swing.JLabel minorFemale;
        private javax.swing.JLabel minorMale;
        // End of variables declaration
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: repetition

    it doesnt display as 2,it still stays 1
    Java Code:
    if(agee > 18) {
        minorMale.setText("" + 1);
    }
    This code doesn't do any addition. Instead it performs "string concatenation": the empty string is joined to a string containing 1. So you are setting the text of minorMale to "1". And you do this every time.

    Instead, consider having an instance variable to represent the number of male minors. Then in the if block you could increment this variable and set the text based on its new value. Something like:

    Java Code:
    if(agee > 18) {
        numMinorMale++;
        minorMale.setText("" + numMinorMale);
    }

  3. #3
    javazers is offline Member
    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0

    Default Re: repetition

    Quote Originally Posted by pbrockway2 View Post
    Java Code:
    if(agee > 18) {
        minorMale.setText("" + 1);
    }
    This code doesn't do any addition. Instead it performs "string concatenation": the empty string is joined to a string containing 1. So you are setting the text of minorMale to "1". And you do this every time.

    Instead, consider having an instance variable to represent the number of male minors. Then in the if block you could increment this variable and set the text based on its new value. Something like:

    Java Code:
    if(agee > 18) {
        numMinorMale++;
        minorMale.setText("" + numMinorMale);
    }
    thank you very much sir,..this helped me a lot,i never really understood how to make it work since its way different from what i learned on my basic programming :) I really appreciate this,thank you a lot

Similar Threads

  1. Method Repetition
    By Gligum in forum New To Java
    Replies: 6
    Last Post: 10-14-2010, 11:56 AM
  2. print random numbers without repetition
    By princess.blue in forum New To Java
    Replies: 3
    Last Post: 02-04-2010, 09:37 AM
  3. Matrix variations with repetition
    By jnovice in forum New To Java
    Replies: 4
    Last Post: 01-19-2009, 08:42 AM
  4. while Loop (Repetition) structure.
    By gapeach97 in forum New To Java
    Replies: 9
    Last Post: 12-03-2008, 11:52 PM
  5. repetition of 'arguments'(?)
    By Igor in forum New To Java
    Replies: 3
    Last Post: 12-13-2007, 10:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •