0

When trying to make a quiz maker code: I want to ask for the number of questions and then create that many questions, and be able to return each question. I'm confused why the output is not line by line output and input. I have imported java.io.* and java.util.*

public class quiz {
    public static void main(String []args){

        Scanner kbReader = new Scanner(System.in);
        System.out.println("~@{@{@{{{{{{{{{{{{{{{{{{  Quizmaker   }}}}}}}}}}}}}}}}}}@}@}@~");
        System.out.println("How many questions are in this quiz?");
        int numberoQuestions = kbReader.nextInt();
        //lets make this input 4
        String question [] = new String [numberoQuestions]; //the questions the user has made
        int createdQs = 0; //how many questions the user has made

        do {
        createdQs ++;
        System.out.println("What is question " + createdQs);
        question [createdQs]= kbReader.nextLine();
        }
        while(createdQs <= numberoQuestions);
/*
 supposed to print
 How many questions are in this quiz?
(4)
 What is question 1?
(input)
 What is question 2?
(input)
 What is question 3?
(input)
 What is question 4?
(input)

it instead prints
How many questions are in this quiz?
4
What is question 1
What is question 2
age?
What is question 3
height?
What is question 4
school?
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
    at finalProject.quiz.main(quiz.java:18)

 */
        System.out.println(question[3] );//prints question 4 but I want it to print question 3
    }
}
Candy
  • 1
  • you should change this createdQs <= numberoQuestions to createdQs < numberoQuestions. to prevent java.lang.ArrayIndexOutOfBoundsException. – Seek Addo Oct 26 '16 at 16:31

2 Answers2

2

Use scanner.readLine() to consume the line break left in the buffer after calling nextInt(), move createdQs++; to the end of the do-while loop, and change the loop condition to createdQs < numberoQuestions (since arrays start from 0 and go to length-1).

Kayaman
  • 67,952
  • 3
  • 69
  • 110
0

When you use scanner for input, after the first input you should always do scanner.nextLine() so that it returns the control back to input, so that further inputs can be taken.

Here is what you need to do:

public static void main(String[] args) {
         Scanner kbReader = new Scanner(System.in);
            System.out.println("~@{@{@{{{{{{{{{{{{{{{{{{  Quizmaker   }}}}}}}}}}}}}}}}}}@}@}@~");
            System.out.println("How many questions are in this quiz?");
            int numberoQuestions = kbReader.nextInt();
            kbReader.nextLine();//<-- this is mandatory whenever you take input from scanner
            //lets make this input 4
            String question [] = new String [numberoQuestions]; //the questions the user has made
            int createdQs = 0; //how many questions the user has made

            do {

            System.out.println("What is question " + createdQs+1);// you can say +1 here and display question 1
            question [createdQs]= kbReader.nextLine();
            createdQs ++;// increment here not at beginning of do. So that index out of bound exception is not there
            }
            while(createdQs < numberoQuestions);// it should be < not  <=

    }
Pritam Banerjee
  • 15,297
  • 10
  • 71
  • 92