I'm trying to get Java to recognize the output of a while loop as a variable and to use that output in further operations.
I wanted to try and upgrade it by letting one player set the word and the other one guess it. The problem came from making the number of dashes equal to the number of letters in the word that the player entered, so I separated the code out, which worked.
But when I put it all back in main, it would not recognize how many dashes are there after the loop finishes; it only recognizes the initial one which is only 1 dash, and so it poses a problem.
EDIT: Thank you so much guys, its my first time on stack overflow, tnx again.
Works like a charm :D
package iB;
import java.util.Scanner;
import java.lang.String;
public class WordGuess {
/**
* #param args
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String secretWord ;
String guess, dash = "-", upWord;
int numGuesses = 0;
int numWord;
final String SENTINEL = "!";
System.out.println("Player 2, please look away. Player 1, please enter the secter word: \n");
secretWord = input.next().toUpperCase().trim();
numWord = secretWord.length();
//System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
for(int dashNum = 1; dashNum < numWord; dashNum++) {
dash += "-" ;
}
System.out.println("WordGuess game!\n");
do {
System.out.println("Enter a letter (" + SENTINEL + "to guess entire word): ");
guess = input.next().toUpperCase().trim();
numGuesses ++;
if (secretWord.contains(guess) && guess.length() == 1) {
upWord = dash.substring(0, secretWord.indexOf(guess));
upWord += guess;
upWord += dash.substring(secretWord.indexOf(guess) + 1, dash.length());
dash = upWord.toUpperCase();
System.out.println(dash);
if (dash.equals(secretWord)) {
System.out.println("You won!\n" + "The secret word is " + secretWord);
System.out.println("You made " + numGuesses + " guesses."); }
} else if (guess.length() >= 2) {
System.out.println("Please only enter one letter at a time! \n"); }
if (guess.contains(SENTINEL)) {
System.out.println("What is your guess? ");
guess = input.next().toUpperCase().trim();
if (guess.equals(secretWord)) {
System.out.println("You won!\n" + "The secret word is " + secretWord);
System.out.println("You made " + numGuesses + " guesses.");
break;
} else {
System.out.println("You Lose!");
System.out.println("The secret word was " + secretWord);
System.out.println("You made " + numGuesses + " guesses.");
break;
}
}
} while(!guess.contains(SENTINEL));
input.close();
}
}
The problem
The following piece of code appears to be trying to show where in a word the correctly chosen letter can be found
if (SecretWord.indexOf(guess) >= 0) {
UpWord = dash.substring(0, SecretWord.indexOf(guess));
UpWord += guess;
UpWord += dash.substring(SecretWord.indexOf(guess) + 1, dash.length());
System.out.println(UpWord);
} else {
So if the word was this and you guessed i then the output should be
--i-
dash.substring does not repeat dash, it takes a sub part of dash, as dash is 1 letter long, anything other than substring(0,1) will lead to an exception.
Basic solution
I believe you want to repeat dash until you get to the guessed letter, and then after it till the end of the word. Something along the lines of:
if (SecretWord.indexOf(guess) >= 0) {
int guessedIndex=SecretWord.indexOf(guess);
String outString="";
for(int i=0;i<guessedIndex;i++){
outString+=dash; //repeat dash until we get to the correctly guessed letter
}
outString+=guess; //put the letter in
for(int i=guessedIndex;i<SecretWord.length();i++){
outString+=dash; //repeat dash until we get to end of the word
}
System.out.println(outString);
} else {
Better Solution
This however leaves the problem that only the first instance of the letter is shown. This can be solved using annother stack overflow answer in which we see that we can get all the occurances of a character using a function
public static ArrayList<Integer> getAllIndexes(String testChar, String string){
int index=string.indexOf(testChar);
ArrayList<Integer> indexes=new ArrayList<Integer>();
while(index>0){
indexes.add(index);
index=string.indexOf(testChar,index+1);
}
return indexes;
}
Then using that function to find all the indexes at which the letter occurs we can deal with repeated letters
if (SecretWord.indexOf(guess) >= 0) {
int guessedIndex=SecretWord.indexOf(guess);
ArrayList<Integer> indexes=getAllIndexes(guess,SecretWord);
String outString="";
for(int i=0;i<SecretWord.length();i++){
if (indexes.contains(i)){
outString+=guess; //if its one of the guessed letters, put that in
}else{
outString+=dash; //otherwise add a dash
}
}
System.out.println(outString);
} else {
Now a word of hello and a guess of l correctly outputs --LL-
Notes
It is usual to follow the naming convention that variable names are
in lower camel case, meaning they start with a lower case letter, as
such SecretWord should be secretWord. As it is currently written
it looks like SecretWord is a class, which are usually writen in
upper camel case.
It would be nice, if once you've guessed a letter it stops putting a dash in and starts putting the letter in every time after that, this could be achieved by using an array of booleans to check if the letter has been guessed but that is beyond the scope of this question
All of these solutions have appended strings, which can be slow for huge numbers, in your case this is the right thing to do, but is joining lots of strings together in a loop consider using a StringBuilder to remove the overhead of creating loads of intermediate strings
Solution
If the secret word is pony, the String dash should be equal to ----. The problem is that you never actually change dash from being equal to -. Therefore, when you do things like dash.substring(SecretWord.indexOf(guess) + 1, dash.length()), you get errors because dash only contains one character. Here's how I'd make dash the same length as the secret word:
for(int i = 0; i < NumWord; i++) {
dash += "-";
}
With this one change inserted directly before your do-while loop, your program works like a charm. Below are some other things to consider in order to further improve your program.
Improving readability
Java convention dictates that the first word of method and variable names is lowercase. So NumWord should be numWord, SecretWord should be secretWord, etc.
SecretWord.indexOf(guess) >= 0 should be changed to
SecretWord.contains(guess)
Gameplay suggestions
As in hang man, you should probably show all the spots where the guessed letter occurs. For example, if the secret word is happy, a guess of p should produce the output of --PP- instead of --P--.
As a rule, never accept bad input even if it doesn't cause errors. The program shouldn't allow any of the scenarios below:
A user enters a String containing non-alphabetic characters or multiple words as the secret word
When making guesses, non-alphabetic characters are input (excluding !)
When guessing letters, multiple characters are input.
I have made a couple of modifications to your code, it seems to work fine now.
First though, I added an extra method, just to make it a little easier:
public static String printOutWord(String[] UpWord){
String out = "";
for(int i = 0; i < UpWord.length; i++){
out += UpWord[i];
}
return out;
}
Here are the first few changes to you code:
String[] UpWord = new String[NumWord];
for(int i = 0; i < NumWord; i++){
UpWord[i] = "-";
}
printOutWord(UpWord);
System.out.println("\nWordGuess game!");
So, you no longer need the variable dash, and the variable UpWord has been changed to an array of Strings.
And this is the rest of it:
do {
System.out.println("Enter a letter (! to guess entire word): ");
guess = input.next().toUpperCase().trim();
Numguesses++;
if(guess.length() > 1){
System.out.println("Please only enter one letter at a time");
}else if (SecretWord.indexOf(guess) >= 0) {
int index = SecretWord.indexOf(guess);
UpWord[index] = guess;
while(SecretWord.indexOf(guess, index+1) >= index){
index = SecretWord.indexOf(guess, index+1);
System.out.println(index);
UpWord[index] = guess;
}
System.out.println(printOutWord(UpWord));
if(printOutWord(UpWord).equals(SecretWord)){
System.out.println("You won!\n" + "The secret word is " + SecretWord);
return;
}
} else {
if (guess.contains("!")) {
System.out.println("What is your guess? ");
guess = input.next().toUpperCase();
if (guess.equals(SecretWord)) {
System.out.println("You won!\n" + "The secret word is " + SecretWord);
System.out.println("You made " + Numguesses + " guesses");
} else if (!guess.equals(SecretWord)) {
System.out.println("You Lose!");
System.out.println("You made " + Numguesses + " guesses");
}
}
}
} while (!SecretWord.equals(guess));
input.close();
}
Most of the changes are within the first if statement.
I hope this helped, if any clarification is needed about anything, just ask, I'd be happy to help :)
Related
I've tried using this code, but everytime it runs and I type in quit, it prints out the result as if it should be a word and not a trigger to end the loop, then the loop ends. I tried just putting in an if then statement where if the word was quit it would end the entire program, but I can't get rid of the do while loop having the condition of word not equaling "quit". Here's the coding.
do
{
System.out.println("Enter a word and I will see if, when the first letter is made the last" + '\n' + "and this word is spelled backwards, is the original word. Type quit to end: ");
word = keyboard.next().toLowerCase();
String newWord = word.substring(1) + word.charAt(0);
String reverse = "";
for(int wordLength = newWord.length() - 1; wordLength >= 0; wordLength--)
{
reverse = reverse + newWord.charAt(wordLength);
}
if (word.equals(reverse)) {
System.out.println("You have entered " + word + ". Does this word with it's first letter made the last then spelled in reverse become the original word? Yes!");
} else {
System.out.println("You have entered " + word + ". Does this word with it's first letter made the last then spelled in reverse become the original word? No");
}
}while (!word.equals("quit"));
As always, I appreciate any help!
The do while loop only checks the condition when it reaches the end of the loop. Therefore, what is happening in your code is, when word is set to "quit" the code below it still runs until it reaches the end of the loop.
To achieve what you want to do try this,
do
{
System.out.println("Enter a word and I will see if, when the first letter is made the last" + '\n' + "and this word is spelled backwards, is the original word. Type quit to end: ");
word = keyboard.next().toLowerCase();
if(word.equals("quit")){ break; }
String newWord = word.substring(1) + word.charAt(0);
String reverse = "";
for(int wordLength = newWord.length() - 1; wordLength >= 0; wordLength--)
{
reverse = reverse + newWord.charAt(wordLength);
}
if (word.equals(reverse)) {
System.out.println("You have entered " + word + ". Does this word with it's first letter made the last then spelled in reverse become the original word? Yes!");
} else {
System.out.println("You have entered " + word + ". Does this word with it's first letter made the last then spelled in reverse become the original word? No");
}
}while (true);
This will cause the for loop to stop before is outputs the text when quit is entered.
You can change all of this with simple while loop. Like this:
while(true) {
System.out.println("Enter a word and I will see if, when the first letter is made the last" + '\n' + "and this word is spelled backwards, is the original word. Type quit to end: ");
word = keyboard.nextLine().toLowerCase();
if (word.contentEquals("quit")) break;
String newWord = word.substring(1) + word.charAt(0);
String reverse = "";
for(int wordLength = newWord.length() - 1; wordLength >= 0; wordLength--)
{
reverse = reverse + newWord.charAt(wordLength);
}
if (word.equals(reverse)) {
System.out.println("You have entered " + word + ". Does this word with it's first letter made the last then spelled in reverse become the original word? Yes!\n");
} else {
System.out.println("You have entered " + word + ". Does this word with it's first letter made the last then spelled in reverse become the original word? No\n");
}
}
This will run until the user enters "quit".
if (word.contentEquals("quit")) break;
This will break the loop and the program will stop.
The simplest solution would be to replace your else statement with an if else checking for the word being quit, so the if-else part looks like this:
if (word.equals(reverse)) {
System.out.println("You have entered " + word + ". Does this word with it's first letter made the last then spelled in reverse become the original word? Yes!");
} else if(!word.equals("quit")) {
System.out.println("You have entered " + word + ". Does this word with it's first letter made the last then spelled in reverse become the original word? No");
}
I want my input to be a minimum of 4 strings and a maximum of 8. I am tokenizing them for practice. My loop will rerun if I the input is outside of these parameters, but will only rerun once and continue on with the rest of the program. I feel like I am using the wrong loop for validation because I would just have while/if to infinity at this point I think.
Finally, at the end, when trying to determine how many chars are upper or lower case, my results returns a count of 0, but my letters, digits and white spaces counts are accurate.
I really appreciate any insight.
Here is my code:
import java.util.ArrayList;
import java.util.Scanner;
import java.util.StringTokenizer;
public class charEvaluation {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<String> tokenizedInput = new ArrayList<>();
String sentenceRetrieved;
// getting the sentence from the user
System.out.println("Please type a sentence containing at least 4 words, with a maximum of 8 words: ");
sentenceRetrieved = sc.nextLine();
StringTokenizer strTokenizer = new StringTokenizer(sentenceRetrieved);
// checking to ensure the string has 4-8 words
while (strTokenizer.hasMoreTokens()) {
if (strTokenizer.countTokens() > 8 || strTokenizer.countTokens() < 4) {
System.out.println("Please re-enter a sentence with at least 4 words, and a maximum of 8: ");
//sentenceRetrieved = null;
//tokenizedInput.removeAll(tokenizedInput);
sentenceRetrieved = sc.nextLine();
while (strTokenizer.hasMoreTokens()) {
tokenizedInput.add(strTokenizer.nextToken());
}
} else {
while (strTokenizer.hasMoreTokens()) {
tokenizedInput.add(strTokenizer.nextToken());
}
System.out.println("Thank you.");
break;
}
}
// printing out the sentence
System.out.println("You entered: ");
System.out.println(sentenceRetrieved);
// count the characters in each word
int totalLength = 0;
for (String each : tokenizedInput) {
totalLength += each.length();
System.out.println( each + " has " + each.length() + " characters.");
}
System.out.println("The total number of characters entered without spaces: "+
sentenceRetrieved.replace(" ", "").length());
/*
* Setting up a character array and determining how many
* letters, digits, lower case letters, upper case letters and white spaces in the input.
*/
char [] array;
int letters = 0,
digits = 0,
lowerCase = 0,
upperCase = 0,
whitespaces = 0;
array = sentenceRetrieved.toCharArray();
for(int i = 0; i< array.length; i++) {
if (Character.isLetter(array[i]))
letters ++;
else if(Character.isDigit(array[i]))
digits++;
else if(Character.isUpperCase(array[i]))
upperCase++;
else if(Character.isLowerCase(array[i]))
lowerCase++;
else if(Character.isWhitespace(array[i]))
whitespaces++;
}
System.out.println("The number of letters is " + letters + ".");
System.out.println("The number of digits is " + digits + ".");
System.out.println("The number of lower case letters is " + lowerCase+ ".");
System.out.println("The number of upper case letters is " + upperCase + ".");
System.out.println("The number of white spaces is " + whitespaces + ".");
}
}
My output then looks like this:
Please type a sentence containing at least 4 words, with a maximum of 8 words:
hi there3
Please re-enter a sentence with at least 4 words, and a maximum of 8:
hi there3
You entered:
hi there3
hi has 2 characters.
there3 has 6 characters.
The total number of characters entered without spaces: 8
The number of letters is 7.
The number of digits is 1.
The number of lower case letters is 0.
The number of upper case letters is 0.
The number of white spaces is 1.
How about something like this. It will keep prompting until the conditions are met.
while (true) {
System.out.println(
"Please type a sentence containing at least 4 words, with a maximum of 8 words: ");
sentenceRetrieved = sc.nextLine();
StringTokenizer strTokenizer =
new StringTokenizer(sentenceRetrieved);
int count = strTokenizer.countTokens();
if (count <= 8 && count >= 4) {
break;
}
}
To fix your second problem, get rid of the first else. Once you determine it is a letter, you don't do any other testing. You can also just test for upper/lower once you figure out it's a letter.
if (Character.isLetter(array[i]))
letters++;
if (Character.isDigit(array[i])) {
..
} else if(
If isLetter, it can be upper or lower. Try this.
if (Character.isLetter(array[i])){
letters ++;
if(Character.isUpperCase(array[i]))
upperCase++;
else if(Character.isLowerCase(array[i]))
lowerCase++;
} else if(Character.isDigit(array[i])){
digits++;
} else if(Character.isWhitespace(array[i]))
whitespaces++;
In your code, you checked if isUpper or isLower only if !isCharacter which is wrong I believe.
That is why your counts were not correct
Alright, so for a class I am taking I have to make a program that tests tweets. It asks you to input a tweet, then tells you if the tweet is valid (less than 140 characters), tells you the amount of mentions (indicated by the character #) and the number of hashtags (indicated by a #), and tells you whether or not it is a retweet (if it contains "RT:" it is considered a retweet).
I can tell whether it is a valid tweet and can tell if it is a retweet (I coded it so that if the index of "RT:" is greater than or equal to 0, it says it is a retweet), but can't figure out how to count the number of # and # in the string the user enters. I know how to find the index, but am having trouble finding out where to go from there. I don't know what to do as a next course of action. Is there a way to count the amount of a certain character in a string?
I know what the code is currently doing, outputting the index of the first time the character shows up, but I am lost on what else I could do. I thought that maybe I could truncate every letter before and including the # and use a loop to count the amount of times that I get an index for #, then do the same for the #, but I don't know how to truncate every letter before and including a certain character. Or is there a better option? Any help is appreciated
import java.util.Scanner;
import java.lang.Math;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Please enter a tweet:");
String s = scan.nextLine();
int length = s.length();
if(length > 140)
System.out.println("Excess Characters: " + ( length - 140));
else{
System.out.println("Length Correct");
int at = s.indexOf('#');
System.out.println("Number of Mentions: " + (at));
int hash = s.indexOf('#');
System.out.println("Number of Hashtags: " + (hash));
if (s.indexOf("RT:") >=0)
System.out.println("The input was a retweet.");
else
System.out.println("The input was not a retweet.");
}}}
.indexOf will return the index of that character in your String.
That might not be the best approach to resolve your problem.
You could do something like this :
for(int i=0; i<s.length(); i++) {
if(s.charAt(i) == '#') {
count++; //or whatever mechanism you want to keep track of those chars.
}
}
Improving slightly on Caleb's answer:
Since you know which two characters you need to count, '#' and '#,' you can have a counter for each and just iterate over the tweet once. Then you just check if a character is one you're looking for, and if it is, the counter is incremented!
int mentions = 0;
int hashtags = 0;
for(int i = 0; i < s.length; i++) {
if(s.charAt(i) == '#') {
mentions++;
} else if(s.charAt(i) == '#') {
hashtags++;
}
}
Now mentions and hashtags should have the countes of #'s and #'s respectively.
You can solve this problem by implementing a simple counting method:
public int charCount(char c, String tweet) {
int count = 0;
for(int i = 0; i < tweet.length()) {
if(tweet.charAt(i) == c) count++;
}
return count;
}
With this, you can count the number of times a character appears in a tweet.
System.out.println("Length Correct");
int at = charCount('#', s);
System.out.println("Number of Mentions: " + at);
int hash = charCount('#', s);
System.out.println("Number of Hashtags: " + hash);
if (s.indexOf("RT:") >= 0)
System.out.println("The input was a retweet.");
else
System.out.println("The input was not a retweet.");
I have the majority of my program finished, but now that I have most of the code it is tough to find the errors. I have multiple errors at the moment, but the main error I really need help with is that my program will loop the same guess over & over if it is correct. It is in an infinite loop, & I cannot find where it is. This has also brought to my attention that my program will go into negative guesses as it is supposed to end when it gets to 0. Some other errors that would be nice to get help with is 1) it shows a correct guess as an incorrect guess 2) it can only replace one letter in the secret word if there are multiple it will give me an error & end the program. & 3) if I enter 9 to quit, it does not quit.
Thanks in advance for any help. I can add code if needed ( I am only posting the main body ATM.)
public static final int DICTIONARY = 15000;
public static final int GUESSES = 8;
public static final int SECRETLENGTH = 20;
public static void main(String[] args) {
int usedSize = 0, randomWord, guesses = GUESSES;
String word, secretWord, guess, incorrectGuess, correctWord, playAgain;
char letter;
try
{
// Set up connection to the input file
Scanner hangmanDictionary = new Scanner(new FileReader("dictionary.txt"));
String [] dictionary = new String [DICTIONARY];
while (usedSize < DICTIONARY && hangmanDictionary.hasNextLine()) {
dictionary[usedSize] = hangmanDictionary.nextLine();
usedSize++;
}
kbd.nextLine();
clearScreen();
randomWord = pickRandom(DICTIONARY);
word = dictionary[randomWord];
secretWord = secret(word);
//comment out when done testing
System.out.println(word);
System.out.println("Here is the word to guess: " + secretWord);
System.out.println("Enter a letter to guess, or 9 to quit.");
guess = kbd.next();
do {
while (!guess.equals("9") || !(guess.equals(word) && guesses > 0)) {
letter = guess.charAt(0);
incorrectGuess = "";
incorrectGuess += letter;
if (word.indexOf(letter) < 0) {
guesses--;
System.out.println("Incorrect guesses: " + incorrectGuess);
System.out.println("Number of guesses left: " + guesses);
System.out.println("Enter a letter to guess, or 9 to quit.");
guess = kbd.next();
}
else {
//FINSH THIS
correctWord = correctWord(guess, word, secretWord, letter);
System.out.println(correctWord);
System.out.println("Incorrect guesses: " + incorrectGuess);
System.out.println("Number of guesses left: " + guesses);
System.out.println("Enter a letter to guess, or 9 to quit.");
guesses--;
}
}
if (guess.equals("9")) {
System.out.println("Thanks for playing!");
System.exit(0);
}
if (guess.equals(word)) {
System.out.println("You won!");
}
if (guesses == 0) {
System.out.println("You are out of guesses.");
}
System.out.println("Play again? Y/N");
playAgain = kbd.nextLine().toUpperCase();
} while (playAgain.equals("Y"));
}
catch (FileNotFoundException e) {
System.out.println("There was an error opening one of the files.");
}
}
Here's my guess:
Did you forget to put guess = kbd.next(); if the user guessed a correct character?
The inner while loop is your main problem, i.e. think about what happens when you enter a valid letter (guess), in that case the first condition of the while loop OR condition is TRUE (assuming you don't have a 9 in your secret word), so the while loop is entered without entering the second part of the OR condition. After that you enter the else part of the IF statement (since it's a valid guess) but in the else part you're not asking for the next guess, so it returns to the start of the while loop with the same guess and hence infinite loop.
Similarly, if you enter 9 to exit !guess.equals("9") evaluates to FALSE, so the second part of the OR condition is entered, in the second part
!(guess.equals(word) && guesses > 0) evaluates to TRUE (unless the secret word contains a 9) so you enter the WHILE loop which is invalid. etc ...
Try to write small parts of the code using known parameters and then bring it all together, that way it'll be easier to construct and follow the logic.
I am having some trouble creating a program that uses an array to count vowels in names entered by a user. The user should be able to enter up to 1000 names or say "Done" to end the program. Once the user gets to 1000 names or says done, it is supposed to display the total amount of vowels in each name combined.
Here is what I have so far:
import java.util.Scanner;
import java.lang.String;
import java.lang.Math;
public class Countvowels
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
final int LOW ='A';
final int HIGH = 'Z';
int[] letterCounts = new int[HIGH-LOW+1];
String[] word = new String[1000];
char[] wordLetter;
int offset;
System.out.println("Enter a name: ");
for(int letter = 0; letter < wordLetter.length; letter++){
word[letter] = input.nextLine();
wordLetter = word.toCharArray();
}
}
}
Help is much appreciated!
Never mind all that code. You only need one line:
String[] word = new String[1000]; // given this
int vowels = Arrays.toString(word).replaceAll("(?i)[^aeiou]", "").length();
This first converts the array to a string (basically a csv), then replaces all non-vowels (fyi (?i) is the case-insensitive flag) with nothing (ie deleting them), then with only vowels left just take the length.
If you need a total for all vowels #Bohemian has an excellent answer. If you need them seperate it might be easier to do the following:
Just create 1 big string with all the user input.
Then for example when you end up with:
String userInput = 'JohnMaryLisaPeter';
for(int x = 0; x <= userInput.length() - 1; x++) {
if(userInput.charAt(x) == 97)
vowelA++;
else if(userInput.charAt(x) == 101)
vowelE++;
else if(userInput.charAt(x) == 105)
vowelI++;
else if(userInput.charAt(x) == 111)
vowelO++;
else if(userInput.charAt(x) == 117)
vowelU++;
}
System.out.println("There were " + vowelA + " A's in all your names.");
System.out.println("There were " + vowelE + " E's in all your names.");
System.out.println("There were " + vowelI + " I's in all your names.");
System.out.println("There were " + vowelO + " O's in all your names.");
System.out.println("There were " + vowelU + " U's in all your names.");
Three errors I see:
You have the array-of-characters
char[] wordLetter;
in which the vowels will go, yet you're using it as the for-loop termination. There's nothing in the array yet--there is no array at all, just a marker in memory where it will be created--so you are comparing letter against nothingness!
for(int letter = 0; letter < wordLetter.length; letter++){
It should be
for(int letter = 0; letter < [some_number_here]; letter++){
In the for-loop, you are attempting to change the entire array of words to a character array, which makes no sense. Naming the array of words word is confusing you. Try aWords or something.
wordLetter = word.toCharArray();
Fix:
wordLetter = word[letter].toCharArray();
And letter is another bad choice of variable names. Try iIndex.
I hope this helps!