-1

I am writing a basic code which will be taking the number of question as input. After that I am using a for loop to Read each question. However, It is not asking for the first question and directly moving to the second question. Here is the code :

import java.util.Scanner;
class quiz
{
    static Scanner in = new Scanner(System.in);
    public static void main(String args[])
    {
        int numberofquestion,i;
        String[] question;
        question = new String[200];
        System.out.print("Enter the number of Question : ");
        numberofquestion = in.nextInt();
        System.out.println("The number of question you entered is : "+numberofquestion);
        for(i=0;i<=numberofquestion-1;i++)
        {
            System.out.print("Enter the Question : ");
            question[i] = in.nextLine();
        }
        for(i=0;i<numberofquestion;i++)
        {
            System.out.print("Question = "+question[i]);
        }
    }
}
Marty McVry
  • 2,765
  • 1
  • 13
  • 20
Nitesh Yadav
  • 51
  • 1
  • 9

1 Answers1

2

One solution is to add

 in.nextLine();

after the in.nextInt();

Why? Because nextInt() doesn't read the "newline" character in the input, so your nextLine() in the loop was consuming it.

Christian
  • 31,246
  • 6
  • 45
  • 67