0

In a learning program (I don't know much about this. It was to learn java) the output doesn't stop to get an input on the second iteration of the method which I have called getQ as this is a pub quiz.

This is the code:

import java.util.Scanner;
public class pubQuizArray {
private static Scanner kb = new Scanner (System.in);
static String[] questions;
static String[][] answers;
static char ans;
static char yn;
static char[] correctAns;
static int questionNum;
static int questionNumArray;
static int numQ;
static int score;

public static void writeQuiz()
{
    getQNum();
    getQ();
}

public static void getQNum()
{
    System.out.println("How many Questions?");
    numQ = kb.nextInt();
    questions = new String[numQ];
}

public static void getAns()
{
    answers = new String[numQ][6];
    System.out.println("What are the answers?");

    System.out.print("a: ");
    answers[questionNum][0] = kb.nextLine();

    System.out.print("b: ");
    answers[questionNum][1] = kb.nextLine();

    System.out.print("c: ");
    answers[questionNum][2] = kb.nextLine();

    System.out.print("d: ");
    answers[questionNum][3] = kb.nextLine();


    correctAns = new char[numQ];
    System.out.println("What is the correct Answer?");
    correctAns[questionNum] = kb.next().charAt(0);

}

public static void getQ()
{
    questionNum = 0;
    System.out.println("What is the First Question?");
    questions[questionNum] = kb.nextLine();
    questions[questionNum] = kb.nextLine();
    getAns();
    questionNum ++;
    while(questionNum < numQ)
    {
        System.out.println("What is the next Question?");
        questions[questionNum] = kb.nextLine();
        getAns();
        questionNum ++;
    }
}

public static void askQ()
{
    questionNum = 0;
    score = 0;
    do
    {
        System.out.println("Q" + (questionNum + 1) +": " + questions[questionNum]);

        System.out.println("a: " + answers[questionNum][0]);
        System.out.println("b: " + answers[questionNum][1]);
        System.out.println("c: " + answers[questionNum][2]);
        System.out.println("d: " + answers[questionNum][3]);

        ans = kb.next().charAt(0);
        if(ans == correctAns[questionNum])
        {
            System.out.println("That was correct");
            score ++;
        }
        else
        {
            System.out.println("That was incorrect");
        }
        questionNum ++;
    }while(questionNum < numQ);
}

public static void menu()

{
    System.out.println("Would you like to write a new Quiz? y/n");
    yn = kb.next().charAt(0);
    while(yn == 'y')
    {
        writeQuiz();
        System.out.println("Would you like to play the Quiz? y/n");
        yn = kb.next().charAt(0);
        while(yn == 'y')
        {
            askQ();
            System.out.println("Would you like to play again? y/n");
            yn = kb.next().charAt(0);
        }
    }
}

public static void main(String[] args)
{
    menu();
}
}

and this is the output

Would you like to write a new Quiz? y/n
y
How many Questions?
2
What is the First Question?
asdf
asdf
What are the answers?
a: asd
b: as
c: a
d: adfs
What is the correct Answer?
a
What is the next Question?
What are the answers?
a: 

as you can see when it asks what the second question is it doesn't allow an input. please remember that this just a project to learn java.

  • Although that is not the exact duplicate, but the behaviour is same for `Scanner.next()`. Alternatively, you can replace each `kb.next().charAt(0);` with `kb.nextLine().charAt(0);`. Then you'll not face that issue. – Rohit Jain Aug 21 '13 at 16:38

1 Answers1

0

I am not entirely sure what is causing this, but looking at the javadoc comment

Advances this scanner past the current line and returns the input that was skipped. This  
method returns the rest of the current line, excluding any line separator at the end. The 
position is set to the beginning of the next line. 

my guess is it, when nextLine() is called in between a line, it try to return anything present on the previous line (excluding the newline character) and position the scanner on the next line.

I tried to print the length of the string return by nextLine() call and it was 0.

Just to check whether it is really returning anything, I provided input as

 What is the correct Answer?
 a b 

and nextLine() call return " b" string (note space is required otherwise entire input will be treated as one token and will be read by kb.next()).

To solve your problem you can use kb.nextLine().chatAt(0) when reading the answer from the user.

System.out.println("What is the correct Answer?");
correctAns[questionNum] = kb.nextLine().charAt(0);

Hope this helps.

Prashant Kalkar
  • 524
  • 1
  • 6
  • 17