Hi, I'm new here. I have been looking around for the last few months. Very new to java and I need help with this assignment. I am to program hangman, I know that this has been asked multiple times. I just cannot seem to find the answers that I need. This is what I have so far. I can't seem to figure out what I need to do to print the letters in the correct underscore. I was thinking the block comment section can be used to search for used letters. I think that is all that I have to do in my assignment, and it's the stuff that I don't understand or cannot think of a way to do it. Any help would be greatly appreciated.
Thanks in advance

import java.util.*;
import java.io.*;

public class Assig3
{
	
	
	public static void main(String [] args) throws IOException
	{
	boolean winYes;
	int wins;
	int i;
	int guesses = 7;
	char [] letters = new char [26];
	int index; 
	char lettersGuessed;
	int Wordquit = 2;
	int missWord = 0;
	
	
	Scanner inScan = new Scanner(System.in);
		System.out.println("Please enter your name\n");
		Name.getName(inScan.next());
		//Loads the file as specifed by user
		wordServer theWords = new wordServer();
		System.out.println("Enter file name: ");
		Scanner keyScan = new Scanner(System.in);
		String fName = keyScan.next();
		File inFile = new File(fName);
		Scanner fScan = new Scanner(inFile);
		theWords.loadWords(fScan);
	
		// creates current word object
		String current; 
		current = theWords.getNextWord();
		StringBuilder guessedWord = new StringBuilder();
		
		//Shows user underscores of letters needed.
	do
		{
		System.out.print("Your word is: ");
		i = 0;
		
		while( i < current.length())
			{
				i++;
				guessedWord.append('_');
			}
			
				System.out.println(guessedWord);
				index = 0;
		while(guesses > 0)
			{
				System.out.println("You have " + guesses + " left."); // tells user number of guesses left
			}
		for(i = 0; i <= index - 1; i++)
			{
				System.out.println(letters[i]);
			}
		
		//prompts user to make a guess.
		System.out.println("Please make a guess: ");
		lettersGuessed = inScan.nextChar();
/*	
				char guess = letter.charAt(0);
				boolean correctGuess = false;
	for(int k = 0; k < current.length(); k++)	
		{
			if(guess == digit[i])
				{
					hidden[i] = guess;
					correctGuess = true;
				}
		}
*/			
		
		letters[index] = lettersGuessed; 
		index++;
			
		
		System.out.println("Enter 0 to quit word: ");
		System.out.println("Enter 1 to quit word: ");
		wordQuit = inScan.nextInt();
		while(wordQuit = 2);
		
		
	}
}
import java.util.*;
import java.io.*;

public class wordServer
{
	String [] words;
	int num;
	int [] usedInd;
	int i;
	String empty = "  ";
	
	public wordServer()
		{
			 i = 0;
		}
	
	public void loadWords(Scanner S) throws IOException // loads "dictionary" of words.
		{	
			num = S.nextInt();
			words = new String [num];
			usedInd = new int [num];
			for (int i = 0; i < num; i++)
			words[i] = S.nextLine();
			S.close();
		}
	
	public String getNextWord() // Finds a random index number. then returns a random word.
	{

	do
		{
			int ranInd = (int)(words.length * Math.Random()) + 1
				int hit = 0;
		for(int j = 0; j <= i - 1; j++)
			{
			if(ranInd == usedInd[j])
				{
				hit = 1;	
				}
			}
			while(hit = 1);

				usedInd[i] = ranInd;
				i++;
				return words[ranInd]
			}
	}
}

I also have one last class which is hangPlayer. All this has is holding information of the player. name, wins, misses. And then a StringBuilder which returns the information in a neat and organized way.

Let me get this straight before I dive into trying to help you. So you are displaying the String as "_ _ _ _" initially, where every second character is a space? Then once the user guesses a correct letter, you replace the String with it? (So like "_ _ A _" after they correctly guess A)? If you aren't doing it like that then how are you saving the correct String? And how are you saving the String that you display to the user?

I display the string as underscores. Almost exactly as you have. Instead of it being "_ _ _ _" there would be no space between letters "____". When A was guessed it would show "__A_". <----- this is what I do not understand how to do.
Also what do you mean by saving the string that you display to the user. As in what is holding the underscores?

To make it easier on yourself, you should have two Strings. One String should contain the correct word. The other String should contain what you are displaying to the user. If you do this, when the user guesses a letter, you can say

if (string.contains("A"){
// In here use the String class's replaceAll(oldChar, newChar) method.
}else{
//guess invalid, do other stuff
}

If you don't know what the replaceAll(oldchar, newChar) method is or how it is used, check http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html

I worked on it some more between studying for other classes. It compiles now but with an error. This is the error.

Exception in thread "main" java.util.InputMismatchException
	at java.util.Scanner.throwFor(Scanner.java:819)
	at java.util.Scanner.next(Scanner.java:1431)
	at java.util.Scanner.nextInt(Scanner.java:2040)
	at java.util.Scanner.nextInt(Scanner.java:2000)
	at Assig3.main(Assig3.java:89)
mt-wireless-pittnet-27-70:assignment3 justin$

This is my code so far.

import java.util.*;
import java.io.*;

public class Assig3
{
	
	
	public static void main(String [] args) throws IOException
	{
	boolean winYes;
	int wins;
	int i;
	int guesses = 7;
	char [] letters = new char [26];
	int index; 
	String lettersGuessed;
	int wordQuit = 2;
	int missWord = 0;
	int k;
	char guessedLetter;
	
	
	Scanner inScan = new Scanner(System.in);
		//Loads the file as specifed by user
		wordServer theWords = new wordServer();
		System.out.println("Enter file name: ");
		Scanner keyScan = new Scanner(System.in);
		String fName = keyScan.next();
		File inFile = new File(fName);
		Scanner fScan = new Scanner(inFile);
		theWords.loadWords(fScan);
	
		// creates current word object
		String current; 
		current = theWords.getNextWord();
		StringBuilder guessedWord = new StringBuilder();
		
		System.out.println("Please enter your name: ");

		k = 0;
		//Shows user underscores of letters needed.
	do
		{
		System.out.print("Your word is: ");
		i = 0;
		
		while( i < current.length())
			{
				i++;
				guessedWord.append('_');
			}
			
				System.out.println(guessedWord);
				index = 0;
		
				System.out.println("You have " + guesses + " left."); // tells user number of guesses left
		for(i = 0; i <= index - 1; i++)
			{
				System.out.println("So far you have guessed: ");
				System.out.println(letters[i]);
			}
		
		//prompts user to make a guess.
		System.out.println("Please make a guess: ");
		lettersGuessed = inScan.next();
		guessedLetter = lettersGuessed.charAt(0);
		StringBuilder correctGuess = new StringBuilder();
		int hit = 0;
		for(int j = 0; j < current.length(); j++)
			{
			if(guessedLetter == current.charAt(j));
				{
				hit = 1;
				guessedWord.setCharAt(j, guessedLetter);
				}
			}
 
			letters[k] = guessedLetter;
			k++;
		
		System.out.println("Enter 0 to quit word: ");
		System.out.println("Enter 1 to quit word: ");
		wordQuit = inScan.nextInt();
		}

		while(wordQuit == 2);
		
		
	}
}
while(wordQuit == 2);

What is that for? ^
And what is your correctGuesses variable for? You never used it.

And the reason you got InputMismatchException is because your scanner tried to read in one type of (String, int, double, etc) but your input did not have that type immediately in it. See here

The reason wordQuit and correctGuesses are there are for the last bit that needs to be done. I plan on using wordQuit for when the users presses 0, or 1. 0 meaning the give up on the word and 1 meaning they quit the program completely. so while wordQuit = 2 the loop will continue. I don't know if I am actually doing that correctly or not. CorrectGuesses well I guess really that doesn't need to be there. It doesn't make much sense. Although I need some way for a correctly guessed word to be counted or missed if it was missed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.