0

My code compiles but once I run it, it asks for user input and then stops. However, the program says it's still running but it doesn't move on to execute the rest of the code and I can't figure out why.

Here's a portion of my code. It stops after I ask the user how many questions they would like to answer. According to my debugger, it specifically stops at "qNum = console.nextInt();" when I enter in a valid input.

import java.util.*;
import java.io.*;

public class QuizBowlRedo implements Quiz {  
    private Player player;     // player object
    private String file;       // name of file
    private int qNum;          // number of questions player wants to answer
    private int qNumFile;      // number of questions in file
    private ArrayList<Question> questionsArr; // holds Question objects
    private boolean questionsAsked[];



// Constructor
public QuizBowlRedo(String fName, String lName, String file) throws FileNotFoundException {
    player = new Player(fName, lName);
    Scanner gameFile = new Scanner(new File(file));
    qNum = numOfQuestionsToPlay();
    qNumFile = maxNumQFile(gameFile);
    questionsArr = new ArrayList<Question>();
    readFile();
    questionsAsked = new boolean[qNumFile];    
}

// asks user how many questions to ask
public int numOfQuestionsToPlay() {
    Scanner console = new Scanner(System.in); 

    // CHECKS FOR VALID USER INPUT
    boolean check = false;
    do {
        try {
            System.out.print("How many questions would you like (out of 3)? ");
            if(console.nextInt() > 3) {
                System.out.println("Sorry, that is too many.");
                check = false;
             }               
            else {
                check = true;
                qNum = console.nextInt();           
            }

        }
        catch(InputMismatchException e) {
           System.out.println("Invalid input. Please try again.");
           console.nextLine(); 
           check = false;  
        }
   }
   while(check == false);
   return qNum; 
}

2 Answers 2

1

You're calling nextInt() at two different places in your program, so if your program goes into the "else" block, then it will wait for the result a second time.

You should call nextInt() only once, and assign the result to a local variable before continuing. You could do it like this.

        System.out.print("How many questions would you like (out of 3)? ");
        int answer = console.nextInt();
        if(answer > 3) {
            System.out.println("Sorry, that is too many.");
            check = false;
         }               
        else {
            check = true;
            qNum = answer;           
        }
Sign up to request clarification or add additional context in comments.

Comments

0

console.nextInt() reads an integer, so you mustn't call it unless you want to read an integer.

Try this:

System.out.print("How many questions would you like (out of 3)? ");
if((qNum = console.nextInt()) > 3) {
    System.out.println("Sorry, that is too many.");
    check = false;
 }               
else {
    check = true;        
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.