I finished reading my first of many java book a couple of days ago and now applying what I learned but it seems harder than I thought.
I am trying to take input from the user and add it to the array and iterate through the array to check if the letter has been used before, if not, then add it, and if it was used before then alert the user to input another letter. From testing it seems as though the the for loop will insert the letter and then fill the rest in with blank spaces, also the while loop will not loop the 3 time ether. I have tried it different ways but it will ether fill it all with the same letter or it wont add the letter. What are my mistakes and what is the best way to fix this?
From testing it seems as though the the for loop will insert the letter and then fill the rest in with blank spaces, also the while loop will not loop the 3 time ether.
There are two things you need to change:
1. Remove the break inside the if block. The break will stop the loop, thats why the loop only loop one time.
2. Replace guess.charAt(0) to guess.charAt(i) . We should use guess.charAt(i) instead of guess.charAt(0) because guess.charAt(0) will only takes the first characters of the String.
Make sure you put guess.charAt(i) inside for loop.
I suggest breaking the task into smaller methods. This makes each method much easier to write and understand. The method contains() for instance should only be a few lines long, looping through 'ary' looking for a match. 'getUniqueLetter()' should loop forever until the response is a single letter and is unique (i.e. not contained in the 'letters' array). Note that this is not a robust solution. 'guess' is not checked to see if it has less than 1 character and not more than 1 character.
Is it better to set the input variable as a string then grab the first char or set the variable as a char? Is it bad practice to set the char a variable first? Which is better to use? Here is an example of what I am asking. Which is better and conforms to the best practices.
Wes James wrote:Which is better and conforms to the best practices.
Considering only the code you gave, neither is better since both do essentially the same thing.
What Carey means (and correct me if I'm wrong) is that the second approach provides you the opportunity to insert logic after the in.next() call to do some input validation. The first way does not. In my opinion, that's not much of a pro. The cons to the second approach is that you have an extra intermediate variable which you have named letter and now you're forced to give the value you're actually interested in a less expressive name of l. To me, that's worse.
How would I do it? If getting exactly one letter from the input was really that important, I'd probably write a method to encapsulate that logic since Scanner does not have any built-in methods to just get a single letter:
Otherwise, if it's sufficient, I would just do this:
I wrote:The cons to the second approach is that you have an extra intermediate variable which you have named letter and now you're forced to give the value you're actually interested in a less expressive name of l. To me, that's worse.
Let me expound on this a bit.
Generally, I try to avoid temporary, single-use variables. The exception is that if it can help me isolate and explain a complex calculation. See http://wiki.c2.com/?IntroduceExplainingVariable to understand the choices you have in cases like that.
The problem with your code example is that the name you chose, letter, is misleading because the input may or may not actually be just a single letter. Your code now has more cognitive load because you have to mentally translate the word "letter" to "it could be a single letter or it could be more than one". On the next line, you declare a variable l to hold the first character of letter. Can you see how that code is a little bit confused in what it's trying to say? It's like saying "Give me a coin" and then someone giving you a handful of coins and then you picking out a single coin from the handful. There's something slightly off in that exchange. You wouldn't say you had "a coin" if you actually had a handful of coins, right?