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
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
BufferedReader br = null;
FileReader fr = null;
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started