线程关闭异常是从Scanner对象无意中抛出的

时间:2015-12-21 18:10:39

标签: java

需要帮助确定原因" flipCards.nextInt();"打破这个脚本。如果我使用Scanner,它并没有告诉我任何事情我只得到如下所示的堆栈跟踪。如果我将代码切换为不使用扫描程序对象,而只是使用system.in.readLine()并将其放入try catch块中,它将完成脚本,但是下面的for循环的每次迭代都会抛出一个IO异常,它只是说线程被关闭了。

public void studyResults() {
    //Display cards
    Scanner flipCards = new Scanner(System.in);

for (int i = 0; i < questionList.length; i++) {
    int value = questionList[i];
    System.out.println(collectionQuestions[value]);
    System.out.println("Press enter when ready to move on.\n");
    flipCards.nextInt(); //I also tried system.in.readLine() here
    flipCards.close();

    System.out.println(collectionAnswers[value]);
    System.out.println("Press enter when ready to move on.\n");

}
}

Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at study.studyResults(flashCards.java:117)
    at flashCards.main(flashCards.java:30)

完整脚本:

import java.io.IOException;
import java.util.Scanner;
import java.util.Random;
import java.io.BufferedReader;

public class flashCards {


    public static void main(String[] args) throws IOException {

        //Set parameters for study
        Scanner scanner = new Scanner (System.in);
        System.out.println("How many cards would you like to study? (1-10)\n");  
        int selection = scanner.nextInt(); 
        scanner.close();

        prepareStudy prepareCards = new prepareStudy();
        prepareCards.prepStudySet(selection);
        int questionsIndex[] = prepareCards.prepStudyGet();

        //Setup Deck
        genCard setupCards = new genCard();
        setupCards.setArray();
        String[] testingQuestions = setupCards.getQuestions();
        String[] testingAnswers = setupCards.getAnswers();

        //work through the randomly chosen cards
        study studyTime = new study();
        studyTime.studySetter(questionsIndex, testingQuestions, testingAnswers);
        studyTime.studyResults();



    }

}

class genCard {

    private String question1 = "This is question one";
    private String answer1 = "This is the answer to question one";
    private String question2 = "This is question two";
    private String answer2 = "This is the answer to question two";
    private String question3 = "This is question three";
    private String answer3 = "This is the answer to question three";
    private String question4 = "This is question four";
    private String answer4 = "This is the answer to question four";
    private String question5 = "This is question five";
    private String answer5 = "This is the answer to question five";
    private String question6 = "This is question six";
    private String answer6 = "This is the answer to question six";
    private String question7 = "This is question seven";
    private String answer7 = "This is the answer to question seven";
    private String question8 = "This is question eight";
    private String answer8 = "This is the answer to question eight";
    private String question9 = "This is question nine";
    private String answer9 = "This is the answer to question nine";
    private String question10 = "This is question ten";
    private String answer10 = "This is the answer to question ten";

    private String[] collectionQuestions = new String[10];
    private String[] collectionAnswers = new String[10];

    public void setArray() {
        collectionQuestions[0] = question1;
        collectionQuestions[1] = question2;
        collectionQuestions[2] = question3;
        collectionQuestions[3] = question4;
        collectionQuestions[4] = question5;
        collectionQuestions[5] = question6;
        collectionQuestions[6] = question7;
        collectionQuestions[7] = question8;
        collectionQuestions[8] = question9;
        collectionQuestions[9] = question10;

        collectionAnswers[0] = answer1;
        collectionAnswers[1] = answer2;
        collectionAnswers[2] = answer3;
        collectionAnswers[3] = answer4;
        collectionAnswers[4] = answer5;
        collectionAnswers[5] = answer6;
        collectionAnswers[6] = answer7;
        collectionAnswers[7] = answer8;
        collectionAnswers[8] = answer9;
        collectionAnswers[9] = answer10;
    }
    public String[] getQuestions() {
        return collectionQuestions;
    }
    public String[] getAnswers() {
        return collectionAnswers;
    }

}

class study {
    private int[] questionList;
    private String[] collectionQuestions;
    private String[] collectionAnswers;

    public void studySetter(int[] questionsIndex, String[] testingQuestions, String[] testingAnswers) {
        questionList = questionsIndex;
        collectionQuestions = testingQuestions;
        collectionAnswers = testingAnswers;

    }
    public void studyResults() {
        //Display cards
        Scanner flipCards = new Scanner(System.in);

        for (int i = 0; i < questionList.length; i++) {
            int value = questionList[i];
            System.out.println(collectionQuestions[value]);
            System.out.println("Press enter when ready to move on.\n");
            flipCards.nextInt(); 
            flipCards.close();

            System.out.println(collectionAnswers[value]);
            System.out.println("Press enter when ready to move on.\n");

        }
    }
}

class prepareStudy {

    private int[] questions;

    //getter
    public int[] prepStudyGet() {
        for (int i = 0; i < questions.length; i++) {
            System.out.println(i);
        }
        return questions;
    }

    //setter
    public void prepStudySet(int selection) {
        if (selection <= 10) {
        questions = new int[selection];
        Random rand = new Random();
        for(int i = 0; i < selection; i++) {
            selection = rand.nextInt(selection);
            questions[i] = selection;
            }
        }
    }
}

0 个答案:

没有答案