Please run the code and see if you can find the issue within it. I need it to wo
ID: 3555236 • Letter: P
Question
Please run the code and see if you can find the issue within it. I need it to work asap! A text file will be needed. I get the following:
Enter a filename containing your wordlist: words.txt
MIGHTY
The word to guess is: ******
Enter a character to guess: m
m
The character m occurs in 1 positions.
The word to guess is now: M*****
Enter your guess: mi
That is not the correct word.
Please guess another letter and try again.
Previous characters guessed: M
Enter a character to guess: Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at osu.cse1223.Project12.getCharacterGuess(Project12.java:169)
at osu.cse1223.Project12.main(Project12.java:62)
THE CODE:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.io.*;
public class Project12 {
public static void main(String[] args) {
try{
boolean playAgain = true;
Scanner in = new Scanner(System.in);
File name = getFileName(in);
Scanner getWords = new Scanner(name);
List<Character> previousChars = new ArrayList<Character>();
ArrayList<String> list = new ArrayList<String>();
list = getList(getWords);
int getRandom = 1;
String guessMe = "";
String wordGuess = "";
while (playAgain)
{
char charGuess = 0;
if(getRandom == 1)
{
guessMe = getRandomWord(list);
getRandom--;
System.out.println(guessMe);
System.out.println();
System.out.println("The word to guess is: " + starWord(guessMe));
}
else
{
if (previousChars.size() > 0)
{
String previous = previousChars.toString().replace("[", "").replace("]", "");
System.out.println();
System.out.println("Previous characters guessed: " + previous.toUpperCase());
}
charGuess = getCharacterGuess(in);
previousChars.add(charGuess);
printCharOccurences(charGuess, guessMe);
modifyGuess(charGuess, guessMe);
wordGuess = getWordGuess(in);
printGuess(wordGuess, guessMe);
playAgain = playAgain(wordGuess, guessMe);
}
if (checkWord(wordGuess, guessMe) == true)
{
getRandom++;
getCharacterGuess(in);
printCharOccurences(charGuess, guessMe);
modifyGuess(charGuess, guessMe);
}
}
}
catch (FileNotFoundException e)
{
System.out.println("File not found.");
}
}
private static File getFileName (Scanner in)
{
System.out.print("Enter a filename containing your wordlist: ");
File wordList = new File(in.nextLine());
return wordList;
}
private static ArrayList<Character> characterList(String guessMe) {
int guessMeLength = guessMe.length();
ArrayList<Character> inList = new ArrayList<Character>();
for (int i = 0; i < guessMeLength; i++)
{
inList.add(guessMe.charAt(i));
}
return inList;
}
private static ArrayList<String> getList(Scanner inScanner) {
ArrayList<String> list = new ArrayList<String>();
while (inScanner.hasNext())
{
list.add(inScanner.nextLine());
}
return list;
}
private static String getWordGuess(Scanner kb) {
System.out.print("Enter your guess: ");
String wordGuess = kb.nextLine();
return wordGuess.toUpperCase();
}
private static boolean checkWord(String guess, String solution) {
boolean a = true;
guess = guess.toUpperCase();
if (guess.length() != solution.length())
{
a = false;
}
else
{
for (int i = 0; i < guess.length(); i++)
{
char g = guess.charAt(i);
char s = solution.charAt(i);
if (g != s)
{
a = false;
}
}
}
return a;
}
public static void printGuess (String guess, String solution)
{
if (checkWord(guess, solution) == false)
{
System.out.println("That is not the correct word.");
System.out.println();
System.out.println("Please guess another letter and try again.");
}
else
{
System.out.println("Congratulations! " + solution + " is the correct word!!!!");
System.out.println();
}
}
private static String getRandomWord(ArrayList<String> inList) {
String random = inList.get((int) (Math.random() * inList.size()));
return random.toUpperCase();
}
private static char getCharacterGuess(Scanner inScanner) {
boolean getValidGuess = true;
char guess = 0;
while (getValidGuess)
{
System.out.print("Enter a character to guess: ");
String charGuess = inScanner.nextLine();
System.out.println(charGuess);
int curLength = charGuess.length();
if (curLength > 1)
{
System.out.println("ERROR!!! You can only guess a single character.");
}
else
{
guess = charGuess.charAt(curLength - 1);
getValidGuess = false;
}
}
return guess;
}
private static void printCharOccurences(char charGuess, String guessMe){
ArrayList<Character> inList = new ArrayList<Character>();;
inList = characterList(guessMe);
int charCount = charCount(charGuess, guessMe);
if(checkInList(charGuess, inList))
{
if (charCount == 1)
{
System.out.println("The character " + charGuess + " occurs in " + charCount + " position.");
System.out.println("");
}
else
{
System.out.println("The character " + charGuess + " occurs in " + charCount + " positions.");
System.out.println("");
}
}
else
{
System.out.println("The character " + charGuess + " occurs in " + charCount + " positions.");
System.out.println("");
}
}
private static boolean checkInList(char inChar, ArrayList<Character> inList) {
boolean checkInList = true;
for(int i = 0; i < inList.size(); i++)
{
if (inList.get(i) == inChar)
{
checkInList = true;
}
else
{
checkInList = false;
}
}
return checkInList;
}
private static String starWord(String inWord) {
int length = inWord.length();
String starWord = "";
for(int i = 0; i < length; i++)
{
starWord += "*";
}
return starWord;
}
private static int charCount(char guessChar, String guessWord) {
int count = 0;
int length = guessWord.length();
for (int i = 0; i < length; i++)
{
if (guessWord.charAt(i) == guessChar || (guessWord.toLowerCase()).charAt(i) == guessChar)
{
count++;
}
}
return count;
}
private static String modifyGuess(char inChar, String word) {
int length = word.length();
String modified = "";
String lastModified = "";
for (int i = 0; i < length; i++ )
{
if (lastModified == "")
{
String wordLC = word.toLowerCase();
if (inChar == word.charAt(i) || inChar == (wordLC).charAt(i))
{
modified += word.charAt(i);
}
else
{
modified += "*";
}
}
else
{
if (lastModified.charAt(i) == '*')
{
String wordLC = word.toLowerCase();
if (inChar == word.charAt(i) || inChar == (wordLC).charAt(i))
{
modified += word.charAt(i);
}
}
}
}
System.out.println("The word to guess is now: " + modified);
lastModified = modified;
return modified;
}
private static boolean playAgain(String guess, String solution) {
boolean a = true;
int guessCount = 1;
Scanner in = new Scanner(System.in);
if (checkWord(guess, solution) == false)
{
guessCount++;
}
else
{
System.out.println("You got it right in " + guessCount + " guesses!");
System.out.print("Would you like to play again [y/n]? ");
String choice = "";
choice = in.next();
if (choice == "y" || choice == "Y")
{
a = true;
}
else if (choice == "n" || choice == "N")
{
System.out.println("Thanks for playing. Goodbye.");
a = false;
}
}
in.close();
return a;
}
}
Explanation / Answer
package com.sampath.chegg;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.io.*;
public class Project12 {
private static String lastModified = "";
public static void main(String[] args) {
try{
boolean playAgain = true;
Scanner in = new Scanner(System.in);
File name = getFileName(in);
Scanner getWords = new Scanner(name);
List<Character> previousChars = new ArrayList<Character>();
ArrayList<String> list = new ArrayList<String>();
list = getList(getWords);
int getRandom = 1;
String guessMe = "";
String wordGuess = "";
while (playAgain)
{
char charGuess = 0;
if(getRandom == 1)
{
guessMe = getRandomWord(list);
getRandom--;
System.out.println(guessMe);
System.out.println();
System.out.println("The word to guess is: " + starWord(guessMe));
}
else
{
if (previousChars.size() > 0)
{
String previous = previousChars.toString().replace("[", "").replace("]", "");
System.out.println();
System.out.println("Previous characters guessed: " + previous.toUpperCase());
}
charGuess = getCharacterGuess(in);
previousChars.add(charGuess);
printCharOccurences(charGuess, guessMe);
modifyGuess(charGuess, guessMe);
wordGuess = getWordGuess(in);
printGuess(wordGuess, guessMe);
playAgain = playAgain(wordGuess, guessMe);
}
if (checkWord(wordGuess, guessMe) == true)
{
getRandom++;
getCharacterGuess(in);
printCharOccurences(charGuess, guessMe);
modifyGuess(charGuess, guessMe);
}
}
}
catch (FileNotFoundException e)
{
System.out.println("File not found.");
}
}
private static File getFileName (Scanner in)
{
System.out.print("Enter a filename containing your wordlist: ");
File wordList = new File(in.nextLine());
return wordList;
}
private static ArrayList<Character> characterList(String guessMe) {
int guessMeLength = guessMe.length();
ArrayList<Character> inList = new ArrayList<Character>();
for (int i = 0; i < guessMeLength; i++)
{
inList.add(guessMe.charAt(i));
}
return inList;
}
private static ArrayList<String> getList(Scanner inScanner) {
ArrayList<String> list = new ArrayList<String>();
while (inScanner.hasNext())
{
list.add(inScanner.nextLine());
}
return list;
}
private static String getWordGuess(Scanner kb) {
System.out.print("Enter your guess: ");
String wordGuess = kb.nextLine();
return wordGuess.toUpperCase();
}
private static boolean checkWord(String guess, String solution) {
boolean a = true;
guess = guess.toUpperCase();
if (guess.length() != solution.length())
{
a = false;
}
else
{
for (int i = 0; i < guess.length(); i++)
{
char g = guess.charAt(i);
char s = solution.charAt(i);
if (g != s)
{
a = false;
}
}
}
return a;
}
public static void printGuess (String guess, String solution)
{
if (checkWord(guess, solution) == false)
{
System.out.println("That is not the correct word.");
System.out.println();
System.out.println("Please guess another letter and try again.");
}
else
{
System.out.println("Congratulations! " + solution + " is the correct word!!!!");
System.out.println();
}
}
private static String getRandomWord(ArrayList<String> inList) {
String random = inList.get((int) (Math.random() * inList.size()));
return random.toUpperCase();
}
private static char getCharacterGuess(Scanner inScanner) {
boolean getValidGuess = true;
char guess = 0;
while (getValidGuess)
{
System.out.print("Enter a character to guess: ");
String charGuess = inScanner.nextLine();
System.out.println(charGuess);
int curLength = charGuess.length();
if (curLength > 1)
{
System.out.println("ERROR!!! You can only guess a single character.");
}
else
{
guess = charGuess.charAt(curLength - 1);
getValidGuess = false;
}
}
return guess;
}
private static void printCharOccurences(char charGuess, String guessMe){
ArrayList<Character> inList = new ArrayList<Character>();;
inList = characterList(guessMe);
int charCount = charCount(charGuess, guessMe);
if(checkInList(charGuess, inList))
{
if (charCount == 1)
{
System.out.println("The character " + charGuess + " occurs in " + charCount + " position.");
System.out.println("");
}
else
{
System.out.println("The character " + charGuess + " occurs in " + charCount + " positions.");
System.out.println("");
}
}
else
{
System.out.println("The character " + charGuess + " occurs in " + charCount + " positions.");
System.out.println("");
}
}
private static boolean checkInList(char inChar, ArrayList<Character> inList) {
boolean checkInList = true;
for(int i = 0; i < inList.size(); i++)
{
if (inList.get(i) == inChar)
{
checkInList = true;
}
else
{
checkInList = false;
}
}
return checkInList;
}
private static String starWord(String inWord) {
int length = inWord.length();
String starWord = "";
for(int i = 0; i < length; i++)
{
starWord += "*";
}
return starWord;
}
private static int charCount(char guessChar, String guessWord) {
int count = 0;
int length = guessWord.length();
for (int i = 0; i < length; i++)
{
if (guessWord.charAt(i) == guessChar || (guessWord.toLowerCase()).charAt(i) == guessChar)
{
count++;
}
}
return count;
}
private static String modifyGuess(char inChar, String word) {
int length = word.length();
String modified = "";
for (int i = 0; i < length; i++ )
{
if (lastModified == "")
{
String wordLC = word.toLowerCase();
if (inChar == word.charAt(i) || inChar == (wordLC).charAt(i))
{
modified += word.charAt(i);
}
else
{
modified += "*";
}
}
else
{
if (lastModified.charAt(i) == '*')
{
String wordLC = word.toLowerCase();
if (inChar == word.charAt(i) || inChar == (wordLC).charAt(i))
{
modified += word.charAt(i);
}
}
}
}
System.out.println("The word to guess is now: " + modified);
lastModified = modified;
return modified;
}
private static boolean playAgain(String guess, String solution) {
boolean a = true;
int guessCount = 1;
Scanner in = new Scanner(System.in);
if (checkWord(guess, solution) == false)
{
guessCount++;
}
else
{
System.out.println("You got it right in " + guessCount + " guesses!");
System.out.print("Would you like to play again [y/n]? ");
String choice = "";
choice = in.next();
if (choice == "y" || choice == "Y")
{
a = true;
}
else if (choice == "n" || choice == "N")
{
System.out.println("Thanks for playing. Goodbye.");
a = false;
}
}
// in.close();
return a;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.