Quiz application - java

Status
Not open for further replies.

Tara!

Solid State Member
Messages
8
Location
Ireland
Hi,

what I'm doing here is creating an application that prepares test questions. It can create multiple choice or essay. I've gotten most of it to work perfectly. I'm just experiencing one problem. For the multi choice class, in the readQuestion method I can enter how many choices I want the answer to have. This is the method:

public void readQuestion()
{
System.out.println("How many choices? ");
numChoices = scan.nextInt();

String[] options = new String[numChoices];

System.out.println("Enter the question: ");
question = scan.next();

for(int index = 0; index < options.length; index++)
{
System.out.println("Enter Choice " + (index + 1) + ": ");
options[index] = scan.next();
}

}


When I call this method in my driver program, the output is as follows:


Enter the number of questions:
1
Enter question type:
m
Question type is m
How many choices?
2
Enter the question:
what package is the scanner class from?
Enter Choice 1: Enter Choice 2:


The problem is that it doesn't allow me to enter 'choice 1' or 'choice 2'.

I can't seem to see where I'm going wrong..maybe a pair of fresh eyes would help?

Thanks in advance =)
 
I compiled and ran your method from the command line and it worked just fine. Are you using Eclipse or some other IDE to execute your program? Try running it manually.
 
I'm running jGrasp, I will give it a go manually and let you know how it comes up! thanks :)
 
Hmmm...:S I just compiled it there again and it seems to be working...whatever I was doing wrong.

Thanks for all the replies :)
 
Okay, just one more thing I'm having a bit of difficulty with :

System.out.println("Enter the question: ");
question = scan.next();


When I read in my question, it only reads in and prints out the first word. I've tried scan.nextLine() but then this doesn't let me enter a question in at all.

If anyone had any ideas I'd be very very grateful...

thanks :)
 
Never Mind!!

problem was the stuff that I didn't have in which is in bold:

public void readQuestion()
{
System.out.println("How many choices? ");
numChoices = scan.nextInt();
scan.nextLine();

String[] options = new String[numChoices];

System.out.println("Enter the question: ");
question = scan.nextLine();

for(int index = 0; index < options.length; index++)
{
System.out.println("Enter Choice " + (index + 1) + ": ");
options[index] = scan.next();
}

}
 
Status
Not open for further replies.
Back
Top Bottom