Question: Building on H11a ; 1. Delete the loop that prints out 10 words from the ArrayList.. 2. Get one random word from the ArrayList. 3.

Building on H11a ;

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.BufferedReader;

import java.io.FileReader;

import java.io.IOException;

import java.util.ArrayList;

import java.util.Random;

public class Tttt {

private final String FILENAME =getClass().getResource("words.txt").getPath(); //file path detection

public static void main(String[] args) {

ArrayList myList=new ArrayList();//initializing array list

Random r=new Random();

Tttt f=new Tttt();

myList=f.getInput();//getting list from getInput() method

int size=myList.size();//getting size of list

System.out.println("First Word is "+myList.get(0));//printing FirstWord of File

System.out.println("Last Word is "+myList.get(size-1));//printing LastWord of File

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

int rand=r.nextInt(4580);

System.out.println("The position is "+rand+" of "+myList.get(rand));//printing position of random word along with the word

}

}

public ArrayList getInput(){

BufferedReader br = null;

FileReader fr = null;

ArrayList ar=new ArrayList();

try {

fr = new FileReader(FILENAME);//reading filepath

br = new BufferedReader(fr);//reading object of file

String sCurrentLine;

while ((sCurrentLine = br.readLine()) != null) {

ar.add(sCurrentLine);//reading currentline in file

}

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if (br != null)

br.close();

if (fr != null)

fr.close();

} catch (IOException ex) {

ex.printStackTrace();

}

}

return ar;

}

}

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!