• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can I print a method that already has print?  RSS feed

 
Brian King
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The below code is from page 82 in Head First Java. Is there a way to add a print statement to the one.bark() and/or two.bark() methods that are called in the GoodDogTestDrive?  When I try System.out.println("The first dog will make the sound " + one.bark()) or something like that, nothing seems to work.  Is it because the method already contains a print statement?  Just curious if anyone has the time.



 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One line 14 of GoodDogTestDrive, you are simply call the method bark() in the instance referenced by one.  So no, you can't add anything between the parentheses except the arguments the method needs, if any.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can call print as many times as you like. If you are not getting any output, it's not because you already called print once.

System.out.println("The first dog will make the sound " + one.bark()) or something like that


This doesn't work because the bark() method doesn't return anything (that's what void means). If you want this to work, in the bark() method return the sound the dog makes rather than printing it in the method.

Alternatively, you could leave bark as it is, and call it with something like:


because the bark method does its own printing.
 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The expression you passed to System.out.println, "The first dog will make the sound " + one.bark(), is invalid expression because the bark() method has a void return value. You can't concatenate void to a String.

This is what you probably want to do:


 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian King wrote:When I try System.out.println("The first dog will make the sound " + one.bark()) or something like that, nothing seems to work.


ItDoesntWorkIsUseless (←click on that link)

It helps if you TellTheDetails -- if you had mentioned that you were getting a compile-time error and pasted in what that error said, people trying to help you could hone in on the problem much more quickly and accurately.
 
Brian King
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!