Question: Stuck on a java assignment where I need to write a method that plays one game of a phrase guess. I need to create a

Stuck on a java assignment where I need to write a method that plays one game of a phrase guess. I need to create a boolean array to track which letters in the phrase are to be displayed. So when a spot is true it displays the letter and when it is false it displays an underscore. These phrases include non letters like commas and apostrophes. Those need to be displayed so the guesses from the user are only letters. Below I included my code where it reads in a text file with 10 phrases. I have it where it selects one of those 10 phrases randomly and returns it as a string. From there I have to write the method that I'm stuck on that creates the boolean array from this random phrase chosen.

public static void main(String[] args) throws FileNotFoundException { Scanner console = new Scanner(System.in); Scanner file = new Scanner(new File("hangman.txt")); String[] phrases = getFile(file); pickPuzzle(phrases); playGame(puzzle); // for(int i = 0; i < phrases.length; i++) { // System.out.println(phrases[i]); // } } public static String[] getFile(Scanner console) throws FileNotFoundException { int token = 0; while(console.hasNextInt()) { token = console.nextInt(); } String[] phrases = new String[token+1]; for(int i = 0; i <= token; i++) { phrases[i] = console.nextLine(); } return phrases; } public static String pickPuzzle(String[] phrases) { String puzzle = ""; Random rand = new Random(); int num = rand.nextInt(phrases.length-1)+ 1; puzzle = phrases[num]; return puzzle; } public static boolean playGame(String puzzle) {

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!