0

Trying to handle an exception. Using an If statement at the recommendation of cricket_007 who has been incredibly helpful! However, still struggling through it. Basically, answering the first string question in my gui, and then incrementing the index counter to get the next question. after the user answers the 7th question, I want to end the method, however I just get the outOfBounds exception. I have tried

if (questionIndex > questions.size()) if (questionsIndex > (questions.size()-1)

and have tried writing it the other way where, if the index is less than the size, ask the questions...and else display "done!" and then implement a "results()" method.

protected void calcPersonnelRisk() {
    if(questionIndex >(questions.size()-1)){
        message.setText("Done");
        System.exit(0);
    }
        //do closing action methods, display total Personnel Risk , end program
    else{
    yesButton.setVisible(false);
    noButton.setVisible(false);
    message.setFont(new Font("Tahoma", Font.BOLD, 14));     
    message.setText(questions.get(questionIndex++));
    enterButton.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
                int r= Integer.parseInt(textField.getText().trim());
                responses.add(r);
                textField.setText("");
                message.setText(questions.get(questionIndex++));                     
        }});

    }

}//end calcPersonnelRisk
Ted
  • 1
  • 1

1 Answers1

0

Sooo, answered my own question (well sort of) I still credit Cricket_007. He led me in the right direction, I just put the code in the wrong place. Solution is moving conditional.

protected void calcPersonnelRisk() {
    yesButton.setVisible(false);
    noButton.setVisible(false);
    message.setFont(new Font("Tahoma", Font.BOLD, 14));
    message.setText(questions.get(questionIndex++));
    enterButton.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
                int r= Integer.parseInt(textField.getText().trim());
                responses.add(r);
                textField.setText("");
                if(questionIndex < questions.size()){
                message.setText(questions.get(questionIndex++));}
                else message.setText("Done");
                //do finishing stuff, move to the next method!
        }});
Ted
  • 1
  • 1