Method in another Class not returning anything

I'm working on a project that creates a Hangman game. The GameManager Class should call the getRandomAnswer method from the AnswerBank Class, which should return a random answer of two nouns from a file called noun_noun.txt (I've placed this file in the first level of the project folder). However, whenever I try to test the code, the console produces nothing after typing "yes" when prompted. Can anyone help?

public class GameManager {
private ArrayList<String> puzzleHistory;
private int numPuzzlesSolved;
private AnswerBank bank;
public GameManager(String fn) throws IOException{
    numPuzzlesSolved = 0;
    puzzleHistory = new ArrayList<String>();
    this.bank = new AnswerBank(fn);
}
public static void main(String[] args) throws IOException {
    GameManager obj = new GameManager("noun_noun.txt");
    obj.run();
}

public void run() throws FileNotFoundException{
    Scanner scan = new Scanner(System.in);
    System.out.println("Would you like to play Hangman? Please type 'yes' or 'no'.");
    String inputAns = scan.next();
    if(inputAns.equalsIgnoreCase("no")){
        System.exit(0);
    }
    else if(inputAns.equalsIgnoreCase("yes")){
        System.out.println(bank.getRandomAnswer());
        }
    else{
        System.out.println("Invalid response!");
    }
    scan.close();
}

}
public class AnswerBank {
private ArrayList<String> listStrings;
private File gameFile;
public AnswerBank(String fileName) throws IOException{
    File gameFile = new File(fileName);
    this.gameFile = gameFile;
    this.listStrings = new ArrayList<String>();
}
public String getRandomAnswer() throws FileNotFoundException{
    Scanner fileScan = new Scanner(gameFile);   
    int totalLines = 0;
    while(fileScan.hasNextLine()){
        totalLines++;
    }
    for(int i = 0; i < totalLines; i++){
        this.listStrings.add(fileScan.nextLine());
    }
    int randInt = (int)(Math.floor ( Math.random() * totalLines));
    String randAns = listStrings.get(randInt);
    fileScan.close();

    return randAns;
}

}

puzzleHistory and numPuzzlesSolved will be used later, so please ignore those. Thanks in advance for any help.

Answers


The problem is at here:

while (fileScan.hasNextLine()) {
    totalLines++;
}

You file scanner never move position in the file, so it will keep scan the first line, and since first line is not empty, this is an infinite loop here.

A simple fix here is to count while reading words at the same time:

public String getRandomAnswer() throws FileNotFoundException {
    Scanner fileScan = new Scanner(gameFile);
    int totalLines = 0;
    while (fileScan.hasNext()) {
        totalLines++;
        this.listStrings.add(fileScan.nextLine());
    }
    int randInt = (int) (Math.floor(Math.random() * totalLines));
    String randAns = listStrings.get(randInt);
    fileScan.close();

    return randAns;
}

Hope it helps.


I believe the problem is

while(fileScan.hasNextLine()){
    totalLines++;
}

This is an infinite loop. Try this:

while(fileScan.hasNext()) {
  totalLines++;
}

Need Your Help

android detect touchstart on prototype js

javascript android prototypejs

i'm trying to make a magento extension work on android mobile devices. I bought this extension :

Change column order in deedle frame

c# dataframes deedle

What is the best way to change column order in deedle frame? For example if I have a deedle frame df with columns height, Name, and phone, but I need it in order Name, phone, and height.