Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • 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
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

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 Hand
Posts: 392
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.
 
Saloon Keeper
Posts: 6220
58
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 Hand
Posts: 392
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
Saloon Keeper
Posts: 6220
58
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
Saloon Keeper
Posts: 6220
58
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
Saloon Keeper
Posts: 6220
58
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.

 
Marshal
Posts: 14025
233
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
Marshal
Posts: 14025
233
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?
 
Sheriff
Posts: 24654
58
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.
 
Opportunity is missed by most people because it is dressed in overalls and looks like work - Edison. Tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!