Answers ( 4 )

  1. 2017-01-02 22:01

    You could just place a line separator inside your string. To make sure the program performs well on any platform, you should use the %n formatter and printf your string:

    String[] questions = new String[] {
        // Question #1 with its answers
        "What is your name?%n" +
        "%n"                   +
        "1. Stack%n"           +
        "2. Overflow%n"        +
        "3. John%n"            +
        "4. Doe%n"
    
        // More questions here..
    };
    
    for (String question : questions) {
        System.out.printf(question);
    }
    
  2. 2017-01-02 22:01

    Basically, all you need to do is adding a seperation character in your string and evaluate it on displaying the question.

    String question = "Question\0First Answer\0Second Answer\n with Linebreak\0Third Answer";
    

    To seperate use split():

    String[] questionAndAnswers = question.split("\0");
    

    But this is not how this is supposed to work. You should create a Question object that has properties for questions and possible answers. Then build an array of Question objects instead of String.

    public class Question {
         private String question;
         private String[] answers;
         private int correct;
    
         // have getters and setters here 
    }
    
  3. 2017-01-02 22:01

    If you create a String[] (an array of String objects) then each element within that array can contain any valid Java String, which includes a String containing newline \n characters. So you could have make each array element a multi-line string, each line separated by the \n character, so that the question is found before the first \n, the correct answer is found before the second \n, and the incorrect answers are found in subsequent "lines".

    However, I'd say this actually makes the code more cryptic and thus difficult to follow. A better, object-oriented approach is to create a new class which represents a quiz question and its answers. The outline of this class might look something like this:

    class QuizQuestion {
        String questionText;
        String correctAnswer;
        List<String> incorrectAnswers;
    }
    

    You could leave this class exactly like this, and simply refer to its class fields (from within the same code package), thus creating a new object type which bundles the question text and answers together nicely. Or, if needed, you could add methods to this class, to control/protect the way the class field data is accessed/modified.

    Now instead of working with your original String[] you could work with a List<QuizQuestion> or a QuizQuestion[]. This should make the code easier to read, and easier to enhance in future.

  4. 2017-01-03 00:01

    Although all the answers above are valid implementations to your question, I'd rather go with an object-oriented approach as JB Nizet stated in the comments above. Here is a sample program that implements a quiz and shows an object-oriented implementation. Please don't copy it 1:1 as a solution to your homework. It should rather provide with some examples of what you can do with Java or any other object oriented language...

    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    
    /**
     * Quiz is a "wrapper class" for your whole project. 
     * It contains a set of questions and some helper methods
     * to create the questions and proper answers.
     */
    public class Quiz {
    
        /**
         * We use a list to store our questions. 
         * Each question is an object of type "Question"
         */
        List<Question> questions = new ArrayList<>();
    
        /**
         * The entry point for our program. 
         * Java will run this method, if our code is "started".
         *
         * All it does is
         *
         * 1. Create a new quiz
         * 2. Start the quiz with the 'play' method
         */
        public static void main(String[] args) {
            Quiz quiz = Quiz.create();
            quiz.play();
        }
    
        /**
         * Helper method to create a quiz. 
         * You can add as many questions as you like with as many answers 
         * as you like.
         * The second parameter indicates the index of the answer 
         * that is the expected answer (starting with 1, not with 0).
         */
        public static Quiz create() {
            Quiz quiz = new Quiz();
            quiz.addQuestion(
                    "What is the heaviest animal on earth?",
                    3,
                    "Elephant",
                    "Cow",
                    "Whale",
                    "Crocodile"
            );
            quiz.addQuestion(
                    "What is the biggest planet in the solar system?",
                    2,
                    "Mars",
                    "Jupiter",
                    "Earth",
                    "Saturn"
            );
            return quiz;
        }
    
        /**
         * Helper method that actually starts our quiz.
         *
         * There is a lot of room for improvement here - I just wanted 
         * to give you a brief example
         * of how you can use the code here to play the quiz. Feel free to change :)
         */
        public void play() {
            for (Question q : questions) {
                System.out.println(q.getQuestion());
                int i = 1;
                for (Answer a : q.getAnswers()) {
                    System.out.println(i++ + ". " + a.getAnswer());
                }
                System.out.printf("Please tell me the correct answer: ");
                Scanner in = new Scanner(System.in);
                String givenAnswer = in.next();
                if (q.getExpectedAnswer() == Integer.parseInt(givenAnswer)) {
                    System.out.printf("Brilliant - your answer was correct!");
                } else {
                    System.out.println("Ooops - that was wrong. The expected answer was: " + q.getProperAnswer());
                }
            }
        }
    
        /**
         * Helper method that adds a question to the quiz.
         *
         * First parameter is the question itself.
         * Second parameter is the index of the correct answer 
         *   in the answers given in the third parameter.
         *   Mind that the index starts with 1 not with 0 as arrays do in Java.
         * Third parameter is a variable length parameter: 
         *   this enables you to add as many answers as you want.
         */
        private void addQuestion(String questionText, int expectedAnswer, String... answers) {
            Question question = new Question(questionText);
            for (String answerText : answers) {
                question.addAnswer(new Answer(answerText));
            }
            question.setExpectedAnswer(expectedAnswer);
            this.questions.add(question);
        }
    
        /**
         * Class that implements a question.
         *
         * A question consists of the text for the question itself,
         * the index of the expected answer (starting with 1!) and
         * an ordered list of answers.
         */
        public class Question {
    
            private String question;
    
            private int expectedAnswer;
    
            private List<Answer> answers = new ArrayList<>();
    
            public Question(String question) {
                this.question = question;
            }
    
            public void addAnswer(Answer answer) {
                answers.add(answer);
            }
    
            public void setExpectedAnswer(int expectedAnswer) {
                this.expectedAnswer = expectedAnswer;
            }
    
            public int getExpectedAnswer() {
                return expectedAnswer;
            }
    
            public String getQuestion() {
                return question;
            }
    
            public List<Answer> getAnswers() {
                return answers;
            }
    
            public String getProperAnswer() {
                return answers.get(expectedAnswer - 1).getAnswer();
            }
        }
    
        /**
         * The answer itself is again a class.
         *
         * This is a little over-engineered, it might as well be a string.
         * Implementing it as a separate class enables you to add further 
         *   information - e.g. a scoring for the wrong / right answer...
         */
        private class Answer {
    
            private String answer;
    
            public Answer(String answer) {
                this.answer = answer;
            }
    
            public String getAnswer() {
                return answer;
            }
    
        }
    
    }
    

    If you have any questions regarding the implementation feel free to ask me in the comments. Happy Coding!

◀ Go back