Results 1 to 11 of 11
  1. #1
    Aarix is offline Member
    Join Date
    Jan 2015
    Posts
    6
    Rep Power
    0

    Default Stuck with a logical error

    Hey Guys, I've been at this all day. It seems so simple but I can't get it to connect. First: Here's the entire source.

    Java Code:
      
      import java.awt.FlowLayout;
        import java.awt.event.ActionListener;
        import java.awt.event.ActionEvent;
        import javax.swing.JFrame;
        import javax.swing.JButton;
        import javax.swing.JTextField;
    
         
        //JFrame builds the window
        public class ThreeOne extends JFrame{
               
                int total = 0;
                String sTotal = String.valueOf(total);
             
               
                private JTextField question;
                private JTextField score;
                private JButton Button1, Button2, Button3, Button4, Button5,
                Button6, Button7, Button8, Button9, Button10;
               
                public int currentQuestion = 0;
               
                // The strings are now in an array
                String[] questions = new String[11];
               
                //The window
                public ThreeOne(){
                        super("Exercise 3-1 Ten Leadership Qualities- How Do You Rate?");
                       
                        questions[0] = "Vision: A sense of what could and should be done";;
                        questions[1] = "Ability: Job Knowledge and expertise to achieve results";
                        questions[2] = "Enthusiasm: Personal Commitment that invigorates and motivates people";
                        questions[3] = "Stability : Emotional adjustment and objectivity";
                        questions[4] = "Concern for others: Service to followers and interest in their welfare";
                        questions[5] = "Self-Confidence: inner stregnth that comes from preparation and competence";
                        questions[6] = "Persistence: Determination to see tough tasks through to completion";
                        questions[7] = "Vitality: Stregnth and stamina";
                        questions[8] = "Charisma: Magnetic ability to attract people and cause them to flow";
                        questions[9] = "Integrity: Honesty, stregnth of character, and courage that generates trust";
                        questions[10] = " ";
                        
                        
                        if(total<= 100 && total >= 90){questions[10] = "Excellent; Exceptional!"; }
                        else if(total<= 89 && total >= 80){questions[10] = "High; Very Good";}
                        else if(total <= 79 && total >=70){questions[10] = "Average; Needs Improvement";}
                        else if(total <= 69 && total >= 60 ){questions[10] = "Low; Much work needed";}
                        else if(total <= 59 && total >= 50){questions[10] = "Deficient; Poor";}
                        
                        
                       
                        
                        
                        
                        setLayout(new FlowLayout());
       
                        question = new JTextField(questions[0], 50);
                        question.setEditable(false);
                        
                        add(question);
                       
                        score = new JTextField(sTotal, 5);
                               
                        score.setEditable(false);
                        add(score);
                       
                        Button1 = new JButton("1");
                        Button2 = new JButton("2");    
                        Button3 = new JButton("3");      
                        Button4 = new JButton("4");        
                        Button5 = new JButton("5");
                        Button6 = new JButton("6");
                        Button7 = new JButton("7");            
                        Button8 = new JButton("8");          
                        Button9 = new JButton("9");
                        Button10 = new JButton("10");
                       
                        add(Button1);
                        add(Button2);
                        add(Button3);
                        add(Button4);
                        add(Button5);
                        add(Button6);
                        add(Button7);
                        add(Button8);
                        add(Button9);
                        add(Button10);
                       
                        thehandler handler = new thehandler();
               
                        Button1.addActionListener(handler);
                        Button2.addActionListener(handler);
                        Button3.addActionListener(handler);
                        Button4.addActionListener(handler);
                        Button5.addActionListener(handler);
                        Button6.addActionListener(handler);
                        Button7.addActionListener(handler);
                        Button8.addActionListener(handler);
                        Button9.addActionListener(handler);
                        Button10.addActionListener(handler);
                       
                       
     
                }
               
                //implements means this is the class that handles the events
                private class thehandler implements ActionListener{
                       
                        public void actionPerformed(ActionEvent event){
                               
                                // event is like an enter or a click
                                if(event.getSource().equals(Button1)){
                                    total = total + 1;
                                            nextQuestion();
                                }
                                   
                                if(event.getSource().equals(Button2)){
                                    total = total + 2;  
                                    nextQuestion();
                                   
                                }
                                       
                                if(event.getSource().equals(Button3)){
                                    total = total + 3;
                                    nextQuestion();
                                }
                                       
                                if(event.getSource().equals(Button4)){
                                    total = total + 4;
                                    nextQuestion();
                                }
                                       
                                if(event.getSource().equals(Button5)){
                                    total = total + 5;
                                    nextQuestion();
                                }
     
                                if(event.getSource().equals(Button6)){
                                    total = total + 6;
                                    nextQuestion();
                                }
                                             
                                if(event.getSource().equals(Button7)){
                                    total = total + 7;
                                    nextQuestion();
                                }
                                           
                                if(event.getSource().equals(Button8)){
                                    total = total + 8;
                                    nextQuestion();
                                }
                                             
                                if(event.getSource().equals(Button9)){
                                    total = total + 9;
                                    nextQuestion();
                                }
       
                                if(event.getSource().equals(Button10)){
                                    total = total + 10;
                                    nextQuestion();
                                }
                                                   
                                score.setText(Integer.toString(total));
                               
                                
                        }
                        
                }      
               
                
                public void nextQuestion(){
                     if (currentQuestion >= 10) return;
                     currentQuestion++; 
                     question.setText(questions[currentQuestion]);
                     
                }
                
        }
    The main focus is here:

    Java Code:
     
                              if(total<= 100 && total >= 90){questions[10] = "Excellent; Exceptional!"; }
                        else if(total<= 89 && total >= 80){questions[10] = "High; Very Good";}
                        else if(total <= 79 && total >=70){questions[10] = "Average; Needs Improvement";}
                        else if(total <= 69 && total >= 60 ){questions[10] = "Low; Much work needed";}
                        else if(total <= 59 && total >= 50){questions[10] = "Deficient; Poor";}
    Total IS those numbers. But question[10] will not cooperate with me and it's getting a bit aggravating as I can't find a logical reason for it to be doing that.

    Also I'm unsure of how to perhaps lock the value of total and sTotal after 10 button clicks from any?

    Appreciate any help in advance

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: Stuck with a logical error

    unsure of how to perhaps lock the value of total and sTotal after 10 button clicks
    Not sure what you are asking. What does "lock" mean?
    Do you mean that you don't want the value of those variables to change after 10 clicks?
    You could use a counter to control when the variables are to be updated. Change the counter's value every time a button is clicked.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Aarix is offline Member
    Join Date
    Jan 2015
    Posts
    6
    Rep Power
    0

    Default Re: Stuck with a logical error

    Quote Originally Posted by Norm View Post
    Not sure what you are asking. What does "lock" mean?
    Do you mean that you don't want the value of those variables to change after 10 clicks?
    You could use a counter to control when the variables are to be updated. Change the counter's value every time a button is clicked.
    After the 10th click, or if question[10] is hit, I want total and sTotal to stay at that number regardless of what button was hit

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: Stuck with a logical error

    One way to control updating a variable would be to use a boolean variable: okToUpdate
    If its true, update the variable
    otherwise do not update it.
    Java Code:
      if(okToUpdate) {
          theVariable = <a new value>;   // update the variable if OK
      }
    Set okToUpdate false when you don't want the variable to be updated.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Aarix is offline Member
    Join Date
    Jan 2015
    Posts
    6
    Rep Power
    0

    Default Re: Stuck with a logical error

    Quote Originally Posted by Norm View Post
    One way to control updating a variable would be to use a boolean variable: okToUpdate
    If its true, update the variable
    otherwise do not update it.
    Java Code:
      if(okToUpdate) {
          theVariable = <a new value>;   // update the variable if OK
      }
    Set okToUpdate false when you don't want the variable to be updated.
    Thanks guys I got it with this
    Java Code:
      if(event.getSource().equals(Button1)){
                                   if(currentQuestion < 10){ total = total + 1; nextQuestion();}
                                            
                                }
                                   
                                if(event.getSource().equals(Button2)){
                                	if(currentQuestion < 10){ total = total + 2; nextQuestion();}
                                   
                                }
                                       
                                if(event.getSource().equals(Button3)){
                                	if(currentQuestion < 10){ total = total + 3; nextQuestion();}
                                }
                                       
                                if(event.getSource().equals(Button4)){
                                	if(currentQuestion < 10){ total = total + 4; nextQuestion();}
                                }
                                       
                                if(event.getSource().equals(Button5)){
                                	if(currentQuestion < 10){ total = total + 5; nextQuestion();}
                                }
     
                                if(event.getSource().equals(Button6)){
                                	if(currentQuestion < 10){ total = total + 6; nextQuestion();}
                                }
                                             
                                if(event.getSource().equals(Button7)){
                                	if(currentQuestion < 10){ total = total + 7; nextQuestion();}
                                }
                                           
                                if(event.getSource().equals(Button8)){
                                	if(currentQuestion < 10){ total = total + 8; nextQuestion();}
                                }
                                             
                                if(event.getSource().equals(Button9)){
                                	if(currentQuestion < 10){ total = total + 9; nextQuestion();}
                                }
       
                                if(event.getSource().equals(Button10)){
                                	if(currentQuestion < 10){ total = total + 10; nextQuestion();}
                                }
    Still need to know why the result variable isn't properly changing

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Stuck with a logical error

    Perhaps I am missing something but your if statements for testing total are in the constructor when the value is 0. So I am not certain what you are trying to do.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    Aarix is offline Member
    Join Date
    Jan 2015
    Posts
    6
    Rep Power
    0

    Default Re: Stuck with a logical error

    I need question[10] to equal excellent to poor based on what total is but its always its default " ". I tested it and total DOES equal the proper numbers 0-100 but even with a || operator it still gives won't cooperate.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: Stuck with a logical error

    Can the first if test check the value of currentQuestion and skip ALL of the following if tests?

    Anytime you see a long list of almost identical statements, look for a way to move the common part up a level.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Aarix is offline Member
    Join Date
    Jan 2015
    Posts
    6
    Rep Power
    0

    Default Re: Stuck with a logical error

    Those if statements are telling the program to stop adding total once question[10] has been reached. It DOES add total just fine

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Stuck with a logical error

    Can you include all the code? Clearly this can't run since there is not main entry point.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    Aarix is offline Member
    Join Date
    Jan 2015
    Posts
    6
    Rep Power
    0

    Default Re: Stuck with a logical error

    I discovered the issue. The if statements were'nt in a place where total was properly calculated and I moved the whole if statement and it works now.

Similar Threads

  1. Replies: 5
    Last Post: 08-13-2014, 02:56 AM
  2. Partial GET method in Java Logical Error
    By onursahindur in forum Networking
    Replies: 5
    Last Post: 03-14-2013, 11:46 AM
  3. Stuck on a Do While logical error
    By dougie1809 in forum New To Java
    Replies: 8
    Last Post: 01-23-2012, 06:05 PM
  4. Logical Operator error
    By MBD in forum New To Java
    Replies: 2
    Last Post: 10-11-2011, 01:29 PM
  5. Replies: 3
    Last Post: 09-26-2010, 07:29 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
  •