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.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 ArrayList = getInput(); System.out.println("First Word in the ArrayList: "+ ArrayList.get(0));//first word in the ArrayList System.out.println("Last Word in the ArrayList: "+ ArrayList.get( ArrayList.size()-1));//last word in the ArrayList System.out.println(); Random r = new Random(); int low = 0; int high = 4580; for (int i=0;i<10;i++) { int randomNum = r.nextInt(high - low) + low; System.out.println("The position " + randomNum + " is " + ArrayList.get(randomNum)); } } public static ArrayList 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 wordList = new ArrayList(); //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; } }