• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Understanding why it isn't working properly

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
Ranch Foreman
Posts: 299
4
Android Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 5898
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Randy, I'm afraid that won't work.

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.
 
Randy Tong
Ranch Foreman
Posts: 299
4
Android Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Sorry Randy, I'm afraid that won't work...


Is ok Carey, you're right. My answer doesn't 100% worked. It only partially-solved.
 
Wes James
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I shouldn't set it as a string and then just grab the first character? Is it bad practice to do it like this?  
 
Carey Brown
Bartender
Posts: 5898
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should put it into a String first so that you could check that the String length is *exactly* one character long before proceeding.

I didn't add this check in my example, it is left as an exercise for the programmer.
 
Carey Brown
Bartender
Posts: 5898
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Wes James wrote:I shouldn't set it as a string and then just grab the first character? Is it bad practice to do it like this?  

Could you restructure this sentence slightly to make your meaning clearer? Perhaps an example?
 
Wes James
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.  

 
Carey Brown
Bartender
Posts: 5898
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The second example you wrote, per this............

Carey Brown wrote:You should put it into a String first so that you could check that the String length is *exactly* one character long before proceeding.

 
Sheriff
Posts: 13510
223
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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:
 
Junilu Lacar
Sheriff
Posts: 13510
223
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
Marshal
Posts: 24586
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Generally, I try to avoid temporary, single-use variables. The exception is that if it can help me isolate and explain a complex calculation.



The other exception I make is to support step-by-step debugging through code. So I might write



And then I could put a breakpoint on the second line to see what actually happened in "complex calculation". This is a variation on your exception, I think.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!