• Post Reply Bookmark Topic Watch Topic
  • New Topic

can't quite figure out the logic error in my code  RSS feed

 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have some code that I'm writing in order to learn Java. It's from my code that wants to 'run' a horserace.

The problem is in the for loop.

Here's the code.




And here is the output:

Enter the number of horses in the race: 3
There are 3 horses in the race.
Enter the name of horse 1, between 1 and 18 characters: Ruffian
Your name is 7 letters.
Reenter the name of horse 1, between 1 and 18 characters: ruffian
Horse 1: ruffian
Enter the name of horse 2, between 1 and 18 characters: sssssssssssssssssssssssssssssss
Your name is 31 letters.
Reenter the name of horse 2, between 1 and 18 characters: sssssssssssssssssssssssssssssssss
Your name is 33 letters.
Reenter the name of horse 2, between 1 and 18 characters: sssssssssssssssssssssssssssssssss
Your name is 33 letters.


As you can see even if the name is the right length it still asks you to reenter, which is what i don't want it to do, and then if it's the right length it goes on to the next object in the array. (if it's the wrong length it just goes through the loop until the right length is given, The setName (String horsename) method is in another class and is simply sets the name of the horse using the entry argument here

I managed to fix it after using an ordinary while statement instead of a do statement, but I wanted to submit this because I can't quite grasp what's logically wrong with the code here. I think it has something to do with the entry variable, but I'm not quite seeing it. Can anybody help?

And while I'm here is there any way to write this code using only just one entry statement instead of two, because if there is I haven't figured it out.
 
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is horse?
What is entry?
What is helper?
What is thoroughbred?

The do while loop is executed at least once. It checks its condition after the loop's body. I think you want while loop which checks its condition before the loop is entered.
 
Christopher Laurenzano
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about that: let me post the classes (there are only two):





And here's the thoroughbred class:





horse is an array of thoroughbred objects; entry is the string that is entered in order to give the horse a name (it's 'ready bake code' from Head First Java that allows one to enter data) Helper is another one.

Thoroughbred is the class that contains the horse's name, for now, but I intend to include other variables as I learn more.

Hope that helps.

The code is not finished; just what I have so far, and it will be modified as I learn more, etc.
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot get null from that entry method, so there is no point in checking whether it equals null. Actually that doesn't even do that; it tests whether your String is n‑u‑l‑l, which you can get by entering those four letters.
PP is right. Once you have got the name the first time, enter a while loop as long as the name in incorrect.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christopher Laurenzano wrote:horse is an array of thoroughbred objects; entry is the string that is entered in order to give the horse a name (it's 'ready bake code' from Head First Java that allows one to enter data) Helper is another one.

Seems an odd way to do it to me. And I'm quite sure it didn't suggest that you name classes in lower case. Classes should always start with CAPITAL letters.

If I was doing this, Horse would be a superclass of Thoroughbred (if needed) and Horses (or Stable) would be the collection.

Winston

PS: Your code lines are MUCH too long.
 
Christopher Laurenzano
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Winston; sorry about the long code.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!