-2

What should I do to run each element of my string array in my askQuizQuestion method?

It's a simple quiz but I'm lost on out to initialize each element of the array into my askQuizQuestion method.

Each use response is either a yes or no. If yes then +1 to a running count and after the last question the total count is returned to the printSurveyResults method to determine which response to print out.

public class Quiz {

    public static void printSurveyResults(int answerCount)
    {
        if (answerCount >= 0 && answerCount <= 2)
            {System.out.println ("You are more exhausted than stressed out.");}

        else if (answerCount >= 3 && answerCount <= 5)
            {System.out.println ("You are beginning to stress out.");}      

        else if (answerCount >= 6 && answerCount <= 8)
            {System.out.println ("You are possibly stressed out.");}

        else if (answerCount >= 9 && answerCount <= 12)
            {System.out.println ("You are probably stressed out.");}
    }           

    public static int askQuizQuestion(String prompt, Scanner keyboard)
    {
        int count = 0;
        int i;
        for (i = 0; i < 12; i++);
            System.out.println(prompt);
        if (keyboard.equals("yes"))
            {count++;}

        printSurveyResults(count);
    }   

    public static void main(String[] args) 
    {
        String[] question = new String[12];
        question [0] = "I find myself less eager to go back to work or to resume my chores after a weekend.";
        question [1] = "I feel less and less patient and/or sympathetic listening to other people’s problems.";
        question [2] = "I ask more “closed-ended questions to discourage dialogue with friends and co-workers than “open-ended” ones to encourage it."; 
        question [3] = "I try to get away from people as soon as I can.";
        question [4] = "My dedication to work, exercise, diet, and friendships is waning.";
        question [5] = "I am falling further behind in many of the responsibilities in my life.";
        question [6] = "I am losing my sense of humor.";
        question [7] = "I find it more and more difficult to see people socially.";
        question [8] = "I feel tired most of the time.";
        question [9] = "I don’t seem to have much fun anymore.";
        question [10] = "I feel trapped. ";
        question [11] = "I know what will make me feel better, but I just can’t push myself to do it and I’ll “Yes, but” any suggestions that people make.";
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

0

Find below your code with slight refactoring. It seems your challenge is reading user input in Java. This SO thread: How can I get the user input in Java shows different techniques for doing that. I used the method next() from the Scanner class in the code below.

import java.util.Scanner;

public class Quiz {

    private static final String[] questions = {"I find myself less eager to go back to work or to resume my chores after a weekend.",
            "I feel less and less patient and/or sympathetic listening to other people’s problems.",
            "I ask more “closed-ended questions to discourage dialogue with friends and co-workers than “open-ended” ones to encourage it.",
            "I try to get away from people as soon as I can.",
            "My dedication to work, exercise, diet, and friendships is waning.",
            "I am falling further behind in many of the responsibilities in my life.",
            "I am losing my sense of humor.",
            "I find it more and more difficult to see people socially.",
            "I feel tired most of the time.",
            "I don’t seem to have much fun anymore.",
            "I feel trapped. ",
            "I know what will make me feel better, but I just can’t push myself to do it and I’ll “Yes, but” any suggestions that people make."};

    public static void printSurveyResults(int answerCount) {

        if (answerCount >= 0 && answerCount <= 2) {
            System.out.println("You are more exhausted than stressed out.");
        } else if (answerCount >= 3 && answerCount <= 5) {
            System.out.println("You are beginning to stress out.");
        } else if (answerCount >= 6 && answerCount <= 8) {
            System.out.println("You are possibly stressed out.");
        } else if (answerCount >= 9 && answerCount <= 12) {
            System.out.println("You are probably stressed out.");
        }
    }


    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);  // Reading from System.in
        String response;
        int count = 0;
        for (int i = 0; i < questions.length; i++) {
            System.out.println(questions[i]);
            response = reader.next();
            if (response.equals("yes")) {
                count++;
            }
        }
        printSurveyResults(count);

    }

}
Community
  • 1
  • 1
pelumi
  • 1,530
  • 12
  • 21
  • THANK YOU!!!! I was putting everything in the wrong order along with a list of other issues. I can follow this though. – SpiveyAtticus Oct 06 '15 at 04:09
  • okay, does that imply it solves your problem? Alternatively you can maintain your `askQuizQuestion` function and pass in the index of each element in the array: `for (int i = 0; i < questions.length; i++) {` `askQuizQuestion(questions[i], reader);` – pelumi Oct 06 '15 at 04:16
  • Yes, this has solved my problem. The array iteration was stumping me because I was putting the array under main when it needs its own method to call from. Then iterating through the array. Thank you again. – SpiveyAtticus Oct 06 '15 at 04:22