0

Need help determining why "flipCards.nextInt();" breaks this script. It doesn't tell me anything if I use Scanner I only get the stack trace like below. If I switch the code to not use a scanner object but just use system.in.readLine() and put that in a try catch block it will complete the script but every iteration of the for loop below throws an IO exception that just says the thread was closed.

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)

Full Script:

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;
            }
        }
    }
}
Damisco
  • 85
  • 7
  • Now that's a proper wall of text to scare people off. Maybe condense into a 1-2 sentence question. – takendarkk Dec 21 '15 at 18:12
  • Where does it tell you a _thread was closed_? I think you should also read [What is a stack trace and how can I use it to debug my application errors?](http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors) – Sotirios Delimanolis Dec 21 '15 at 18:12
  • You might find it easier to declare your arrays like this: `String[] mArray = new String[]{ "One", "Two", "Three" };` – Mapsy Dec 21 '15 at 18:26
  • using the stack trace tells me exactly where my issue is I have been unable to determine the why though. I cut the question down to be shorter. Also Alex you are correct I was hoping to complete this script and then look into JDBC and get it into a db so I didn't worry too much about it being ugly just yet – Damisco Dec 21 '15 at 19:51

0 Answers0