Results 1 to 18 of 18

Thread: break statement

  1. #1
    Join Date
    May 2010
    Posts
    18
    Rep Power
    0

    Default break statement

    hello

    does the break statement works with if and else ?

    because i`m trying to use it but there is some error that says the break should be out the switch or loop

    PHP Code:
    if (jRadioButton1.isSelected())
          { 
             
              jLabel2.setText(null);
        
             
    jLabel1.setText("how old am I");
    
    jRadioButton1.setText("15");
    jRadioButton2.setText("16");
    jRadioButton3.setText("17");
    jRadioButton4.setText("18");
           buttonGroup1.clearSelection();
    break;      
          }
          
          else jLabel2.setText("wrong");

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    Indeed, break should only be used in a loop or switch. It cannot be used in if/else.

  3. #3
    Join Date
    May 2010
    Posts
    18
    Rep Power
    0

    Default

    so how it can be solved, i want the if statment to be breaked so the program could go to the next question

  4. #4
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    From the looks of it you don't need the break... just end the if.

  5. #5
    Join Date
    May 2010
    Posts
    18
    Rep Power
    0

    Default

    without the break it can move to the next question and it be for the first question.

  6. #6
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    Can you post the entire code/method? Without knowing exactly what is going on its hard to help.

  7. #7
    Join Date
    May 2010
    Posts
    18
    Rep Power
    0

    Default

    PHP Code:
    
    
    
    public class easy extends javax.swing.JFrame {
    
    
        public easy() {
            initComponents();
        }
    
    
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
            buttonGroup1 = new javax.swing.ButtonGroup();
            buttonGroup2 = new javax.swing.ButtonGroup();
            jLabel1 = new javax.swing.JLabel();
            jRadioButton1 = new javax.swing.JRadioButton();
            jRadioButton2 = new javax.swing.JRadioButton();
            jRadioButton3 = new javax.swing.JRadioButton();
            jRadioButton4 = new javax.swing.JRadioButton();
            jLabel2 = new javax.swing.JLabel();
            jButton2 = new javax.swing.JButton();
            jButton1 = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            addWindowListener(new java.awt.event.WindowAdapter() {
                public void windowOpened(java.awt.event.WindowEvent evt) {
                    formWindowOpened(evt);
                }
            });
    
            jButton2.setText("Next");
            jButton2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton2ActionPerformed(evt);
                }
            });
    
            jButton1.setText("Exit");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
    
            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(19, 19, 19)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jRadioButton2)
                        .addComponent(jRadioButton1)
                        .addComponent(jRadioButton3)
                        .addComponent(jRadioButton4))
                    .addContainerGap(364, Short.MAX_VALUE))
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jLabel1)
                    .addContainerGap(394, Short.MAX_VALUE))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(198, Short.MAX_VALUE)
                    .addComponent(jLabel2)
                    .addGap(206, 206, 206))
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jButton2)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 274, Short.MAX_VALUE)
                    .addComponent(jButton1)
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jLabel1)
                    .addGap(18, 18, 18)
                    .addComponent(jRadioButton1)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jRadioButton2)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jRadioButton3)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jRadioButton4)
                    .addGap(76, 76, 76)
                    .addComponent(jLabel2)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 77, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton2)
                        .addComponent(jButton1))
                    .addContainerGap())
            );
    
            pack();
        }// </editor-fold>
    
        private void formWindowOpened(java.awt.event.WindowEvent evt) {                                  
    
    
    
    
            jLabel1.setText("whats my name?");
            jRadioButton1.setText("aziz");
            jRadioButton2.setText("fahad");
            jRadioButton3.setText("Abdulla");
            jRadioButton4.setText("Khaled");
    
            buttonGroup1.add(jRadioButton1);
            buttonGroup1.add(jRadioButton2);
            buttonGroup1.add(jRadioButton3);
            buttonGroup1.add(jRadioButton4);
            buttonGroup2.add(jRadioButton1);
            buttonGroup2.add(jRadioButton2);
            buttonGroup2.add(jRadioButton3);
            buttonGroup2.add(jRadioButton4);
    
    
        }                                 
    
        private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
          if (jRadioButton1.isSelected())
          { 
             
              jLabel2.setText(null);
        
             
    jLabel1.setText("how old am I");
    
    jRadioButton1.setText("15");
    jRadioButton2.setText("16");
    jRadioButton3.setText("17");
    jRadioButton4.setText("18");
           buttonGroup1.clearSelection();
          
          }
          
          else jLabel2.setText("wrong");
    
    
    
        }                                        
    
    
    
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    System.exit(0);
        }
    
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new easy().setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify
        private javax.swing.ButtonGroup buttonGroup1;
        private javax.swing.ButtonGroup buttonGroup2;
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JRadioButton jRadioButton1;
        private javax.swing.JRadioButton jRadioButton2;
        private javax.swing.JRadioButton jRadioButton3;
        private javax.swing.JRadioButton jRadioButton4;
        // End of variables declaration
    
    }

  8. #8
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    The method ends correctly, why would you need the break?

  9. #9
    Join Date
    May 2010
    Posts
    18
    Rep Power
    0

    Default

    i want to add another if statment so it can be used to the second question.

  10. #10
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    You can use a return statement to end the method.

  11. #11
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    why can't you do smth like

    Java Code:
    if (jRadioButton1.isSelected()) {
     // ...
    } else if (jRadioButton2.isSelected()) {
      // ...
    } else if (jRadioButton3.isSelected()) {
      // ...
    }
    and so forth?

  12. #12
    Join Date
    May 2010
    Posts
    18
    Rep Power
    0

    Default

    great idea, but if you notice the code i have an else statement that says
    PHP Code:
    else jLabel2.setText("wrong");
    and that means in the second question if the true answer was 3 and the user picked the first the label wont be labeled as wrong because in the first question the true answer was the first answer
    Last edited by AbdulAziz Bader; 06-02-2010 at 12:55 AM. Reason: adding

  13. #13
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    well, you dont HAVE to have

    else jLabel2.setText("wrong");


    right?

    do this:

    Java Code:
    boolean answeredCorrectly = true;
    if (jRadioButton1.isSelected()) {
     // ... set answeredCorrectly ...
    } else if (jRadioButton2.isSelected()) {
     // ... set answeredCorrectly ...
    } else if (jRadioButton3.isSelected()) {
     // ... set answeredCorrectly ...
    }
    
    if (!answeredCorrectly) {
      jLabel2.setText("wrong");
    }

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    Or with a chain of if{} else if{} ending the chain with an else{}
    If none of the leading if/else if are true then the last else with be executed.
    Sort of like a default in a switch statement. (The chaining of if/else if is often used that way when the test is not for an int == to something)

  15. #15
    Join Date
    May 2010
    Posts
    18
    Rep Power
    0

    Default

    iluxa, thnx ill try it.

    Norm, sorry didn`t get it, could you explain more ?

  16. #16
    Join Date
    May 2010
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by iluxa View Post
    why can't you do smth like

    Java Code:
    if (jRadioButton1.isSelected()) {
     // ...
    } else if (jRadioButton2.isSelected()) {
      // ...
    } else if (jRadioButton3.isSelected()) {
      // ...
    }
    and so forth?


    i don`t think that will work, because if user in the first question picked the second answer (which is false in the first question and true for the second one) it will move directly to the 3rd question.

  17. #17
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    I guess I don't understand what the program is supposed to do.
    If the user selects the wrong radio button for a question, what is supposed to happen?

    How would the program move directly to the 3rd question, skipping the second question?

  18. #18
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    I think you're logic behind the program is all wrong. How do you determine what question is being answered?

    It doesn't look like you are holding a reference to what is being asked anywhere. I recommend using a variable for holding what question number you are on, such as:
    Java Code:
    int questionNumber = 0;
    You could then use different methods for asking and checking the answer depending on the question Number. Example:
    Java Code:
    //Examples for asking questions...
    private void askQuestion1() {
         jLabel1.setText("whats my name?");
         jRadioButton1.setText("aziz");
         jRadioButton2.setText("fahad");
         jRadioButton3.setText("Abdulla");
         jRadioButton4.setText("Khaled"); 
    }
    
    private void askQuestion2() {
         jLabel1.setText("how old am I");
         jRadioButton1.setText("15");
         jRadioButton2.setText("16");
         jRadioButton3.setText("17");
         jRadioButton4.setText("18");
    }
    
    
    //Examples for checking answers
    private boolean isQuestion1Correct() {
         return jRadioButton1.isSelected();
    }
    
    private boolean isQuestion2Correct() {
         return jRadioButton2.isSelected();
    }
    I would recommend making a method which organizes the asking of questions when the next button is clicked, such as
    Java Code:
    private void askNextQuestion() {
         questionNumber++;
         switch (questionNumber) {
               case 1:
                    askQuestion1();
                    break;
               case 2:
                    askQuestion2();
                    break;
               //And so on and so on.
                default:
                    break;
          }
    You could do the same thing for checking the answers of the question.

    Now when the Submit button or Next button is clicked(Wherever you are trying to verify the answer. It could look like this:

    Java Code:
    private void verifyAnswerAndAsk() {
         if (isQuestionCorrect()) {
               askNextQuestion();
         } else {
                //Set the jlabel to wrong or take other appropriate action
         }
    }

Similar Threads

  1. Misunderstanding of Break
    By Steve Whalen in forum New To Java
    Replies: 6
    Last Post: 10-22-2008, 06:31 PM
  2. Break to certain class
    By Arez in forum New To Java
    Replies: 4
    Last Post: 10-20-2008, 04:13 AM
  3. Break Clock
    By BruenorBH in forum Advanced Java
    Replies: 20
    Last Post: 09-12-2008, 05:27 AM
  4. How to use Break with a label
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:45 PM
  5. How to use Break
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:45 PM

Posting Permissions

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