Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I faced difficulty to complete it the code. 1. Delete the loop that prints out 1

ID: 3716532 • Letter: I

Question

I faced difficulty to complete it the code.

1. Delete the loop that prints out 10 words from the ArrayList.

2. Get one random word from the ArrayList.

3. Make a guess word consisting of underscores. It should be the same length as the word.

3. Ask your user to guess a character in the word. If it is there put the character in the correct spot in the guess word.

4. If it is not there, that is a strike against the user. (10 strikes and he loses).

5. If the user guesses the word, he wins.

import java.io.File;

import java.io.FileNotFoundException;

import java.util.ArrayList;

import java.util.Random;

import java.util.Scanner;

public class H11a {

public static void main(String[] args) {

ArrayList<String> ArrayList = getInput();

       System.out.println("First Word in the Array List is " + ArrayList.get(0));

       System.out.println("Last Word in the Array List is " + ArrayList.get( ArrayList.size()-1));

       System.out.println();

       Random r= new Random();

       int i;

for(i=0;i<10;i++) {

       int randNum= r.nextInt(4581);

System.out.println("the word " + randNum + " is " + ArrayList.get(randNum));

    }

    }

    public static ArrayList<String> getInput() {

    Scanner key = null;

    try {

File file = new File("words.txt");

    ArrayList<String> list = new ArrayList<String>();

    key = new Scanner(file);

    while (key.hasNextLine()) {

    list.add(key.nextLine());

    }

return list;

  }

    catch (FileNotFoundException e)

{

System.out.println("File Not found");

       }

        return null;

    }

}

Explanation / Answer

Program:

import java.io.File;

import java.util.Scanner;

import java.io.FileNotFoundException;

import java.util.ArrayList;

import java.util.Random;

import java.util.Scanner;

import java.util.Arrays;

public class H11A {

public static void main(String[] args) {

ArrayList<String> ArrayList = getInput();

Scanner scan = new Scanner(System.in);

System.out.println("--------Welcome--------");

String word, guessWord;

Random r = new Random();

int low = 0;

int high = ArrayList.size() - 1;

int randomNum = r.nextInt(high - low) + low;

word = ArrayList.get(randomNum);

// to store _

StringBuilder temp;

if (word.length() > 0) {

char[] array = new char[word.length()];

// filling the array with _ based on acyual word length

Arrays.fill(array, '-');

temp = new StringBuilder(new String(array));

} else {

temp = new StringBuilder();

}

int turn = 0;

while (true) {

// checking for wrong guesses

if (turn >= 10) {

System.out.println("Sorry you lose the game, more than 10 wrong attempts");

break;

}

// get the single guess character from console

System.out.print("Enter your letter guess : ");

String guess = scan.nextLine();

if (guess.length() != 1) {

System.out.println("Guesses must each be a single letter.");

continue;

}

char c = guess.charAt(0);

for (int i = word.indexOf(c); i >= 0; i = word.indexOf(c, i + 1)) {

temp.setCharAt(i, c);

}

// increasing the turn value for wrong guess

int k = word.indexOf(c);

if (k == -1) {

turn++;

}

// printing the guess word

System.out.println(temp.toString());

if (word.equals(temp.toString())) {

System.out.println("You guessed correctly!");

break;

}

}

}

public static ArrayList<String> getInput() {

Scanner sacnnerRead = null; // Scanner that can Read

try {

File file = new File("words.txt"); // creat file object based on the file name "words.txt"

if (file.exists()) { // check if file exist or not

ArrayList<String> wordList = new ArrayList<String>(); // ArrayList of String

sacnnerRead = new Scanner(file);// Scanner Read object based on file object

while (sacnnerRead.hasNextLine()) { // Iterating each line in the File

wordList.add(sacnnerRead.nextLine().trim());

}

return wordList;// retirning arraylist

} else {

System.out.println("The input File is " + "" + "not found");

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

} finally {

if (sacnnerRead != null) {

try {

sacnnerRead.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

return null;

}

}