importjava.util.ArrayList;importjava.util.Scanner;classHangman{publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);System.out.println("What word do you want to guess?");Stringword=scanner.next();// Initial guess, only _ArrayList<Character>guess=newArrayList<>();for(inti=0;i<word.length();i++){guess.add('_');}while(guess.contains('_')){System.out.println("Guess a letter:");Stringletters=scanner.next();// scanner.nextChar does not exist :-(charletter=letters.charAt(0);// we read a string and take the first char// Positions to uncoverArrayList<Integer>indexes=getLetterIndexes(word,letter);// Update guess, uncovering guessed lettersuncoverLetters(guess,indexes,letter);// Print what happenedSystem.out.println(guess);}System.out.println("You won the game!");}publicstaticArrayList<Integer>getLetterIndexes(Stringword,charletter){ArrayList<Integer>indexes=newArrayList<>();for(inti=0;i<word.length();i++){if(word.charAt(i)==letter){indexes.add(i);}}returnindexes;}publicstaticvoiduncoverLetters(ArrayList<Character>guess,ArrayList<Integer>indexes,charletter){for(inti:indexes){guess.set(i,letter);}}}