Problème avec un quizz en java

Fermé
Faquaral - 14 avril 2009 à 11:59
flob47800 Messages postés 63 Date d'inscription jeudi 19 juin 2008 Statut Membre Dernière intervention 24 juin 2009 - 14 avril 2009 à 14:11
Bonjour,
J'ai programmé un quizz en Java et le code me renvoie cette erreur quand je le lance:
Error java.util.NoSuchElementException: No line found

Est ce que quelqu'un aurait une solution à mon problème?

Voila le InputFile que j'utilise:

5
The possession of more than two sets of chromosomes is termed?
polyploidy
3
Erling Kagge skiied into here alone on January 7, 1993
south pole
2
1997 British band that produced "Tub Thumper"
Chumbawumba
2
Who is the tallest person on record (8 ft. 11 in) that has lived?
Robert Wadlow
3
I am the geometric figure most like a lost parrot
polygon
1

Et voici mon code:

import java.applet.*;
import java.io.*;
import java.util.Scanner;

public class Trivia extends Applet {

	private String[] questions;
	private String[] answers;
	private int[] values;
	private int score = 0; // Overall score
	private int questionNum = 0; // Track if question 0 - 4

	// Construct the applet
	public Trivia() {

		Scanner inputStream = null;
		int numQuestions = 0;
		try {
			inputStream = new Scanner(new FileInputStream("C:\\trivia.txt"));
			// If we got here, the file was opened
			// Read number of questions

			numQuestions = inputStream.nextInt();

			inputStream.nextLine(); // skip newline
			// Allocate arrays
			questions = new String[numQuestions];
			answers = new String[numQuestions];
			values = new int[numQuestions];
			for (int i = 0; i < numQuestions; i++) {
				questions[i] = inputStream.nextLine();
				answers[i] = inputStream.nextLine();
				values[i] = inputStream.nextInt();
				inputStream.nextLine(); // skip newline
			}
			inputStream.close();
		} catch (Exception e) {
			System.out.println("Error " + e);
			System.exit(0);
		}

	}

	// Returns false if there are no more questions to ask.
	// Otherwise it asks the next question, gets an answer
	// from the user, and displays if the user was correct or not.
	public boolean askNextQuestion() {
		if (questionNum >= questions.length) {
			return false;
		}
		// Show the current question
		System.out.println();
		System.out.println("Question " + questionNum);
		System.out.println(questions[questionNum]);
		Scanner kbd = new Scanner(System.in);
		String guess = kbd.nextLine();
		// Check if the answer is correct or not
		guess = guess.toLowerCase();
		if (guess.equals(answers[questionNum].toLowerCase())) {
			System.out.println("That is correct!");
			score += values[questionNum];
		} else {
			System.out.println("Wrong. The correct answer is "
					+ answers[questionNum]);
		}
		// Go to next question
		questionNum++;
		return true;
	}

	// Displays current score
	public void showScore() {
		System.out.println("Your score is " + score);
	}

	public static void main(String[] args) {
		Trivia t = new Trivia();
		while (t.askNextQuestion()) {
			t.showScore();
		}
		System.out.println("Game over! Thanks for playing!");
	}

	public String getAppletInfo() {
		return "Applet Information";
	}

	// Get parameter info
	public String[][] getParameterInfo() {
		return null;
	}
}


Merci d'avance
Faquaral
A voir également:

3 réponses

flob47800 Messages postés 63 Date d'inscription jeudi 19 juin 2008 Statut Membre Dernière intervention 24 juin 2009
14 avril 2009 à 12:01
java.util.NoSuchElementException est une ligne qui n'existe pas dans ton fichier
"no line found" ligne non trouvée
0
Oui, ça je m'en doutai. Mais mon problème c'est de savoir ce que mon programme essaie de chercher dans mon fichier et ne trouve pas...
0
flob47800 Messages postés 63 Date d'inscription jeudi 19 juin 2008 Statut Membre Dernière intervention 24 juin 2009
14 avril 2009 à 14:11
java g cu mal dsl mé moi c plutot bat ddsl ^^
0