Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

problem with code, wrong output  RSS feed

 
Will Dev
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone,

Could someone please tell me what I am doing wrong with the code below to match the required output.



//required output
null says Ruff!                          //this output i get
last dog’s name is Bart             //this output i get
Fred says Ruff!                        //don't get
Marge says Ruff!                     //don't get
Bart says Ruff!                        //don't get

It's like my loop is not working? or am i suppose to have a separate class file for the Dog when dealing with instance variables ?
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
null says Ruff! //this output i get
  Are you asking why that message has null instead of some name?
That shows that the value of the name variable was null when the bark() method was called.
Where does the code assign a value to the name variable.

Fred says Ruff! //don't get
Marge says Ruff! //don't get
Bart says Ruff! //don't get

Where is the code to create that output?
That print out looks like the bark() method needs to be called for the references that hold the Dog objects with the shown names.

It's like my loop is not working

What is the loop supposed to do?  I see code that increments x by 1 each time it goes around.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Norm mentioned, I  think you forgot to call bark method in loop. To get those last three outputs, you got call bark method on three objects stored in myDogs array like this.
in loop before incrementing value of x;
 
Will Dev
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganish Patil wrote:As Norm mentioned, I  think you forgot to call bark method in loop. To get those last three outputs, you got call bark method on three objects stored in myDogs array like this.
in loop before incrementing value of x;


Ganish you were spot on, thank you. after reviewing the source code again i had left out that line of code myDogs[x].bark(); and i don't understand how i missed it because i reviewed the source code over and over to make sure o never missed anything.

 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will Dev wrote:Ganish you were spot on, thank you. after reviewing the source code again i had left out that line of code myDogs[x].bark(); and i don't understand how i missed it because i reviewed the source code over and over to make sure o never missed anything.
yes happens with eveyone. You are welcome
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!