Not sure why I am getting a void type not allowed error here. All I want to do is print off a line pulling into from my other class.
Kristin Stromberg
Ranch Hand
Joined: May 17, 2005
Posts: 91
posted
0
Hi again. What is the return type of your numberOfMovies() method? My guess is it's void, when it should be a String. Hope this helps...
Lisa Beglaw
Ranch Hand
Joined: Jul 16, 2005
Posts: 65
posted
0
But I don't want anything to return, I just want it to print out.
Lisa Beglaw
Ranch Hand
Joined: Jul 16, 2005
Posts: 65
posted
0
Just looked back at my code for my class MovieCollection and numberOfMovies is a void type.
Kristin Stromberg
Ranch Hand
Joined: May 17, 2005
Posts: 91
posted
0
The problem is you're calling the numberOfMovies() method in your print statement, and unless the numberOfMovies() method returns a string, you'll get the error you're seeing. It's okay if your runTest() method returns void.
Alternatively, maybe you don't have a numberOfMovies() method at all but a numberOfMovies instance variable instead? In that case, you just need to remove the parentheses like this:
Lisa Beglaw
Ranch Hand
Joined: Jul 16, 2005
Posts: 65
posted
0
I double checked my methods and it is definately a method with a void data type.
Is there anyway I can call a method that has void return?
Here is the method I am trying to call...
Kristin Stromberg
Ranch Hand
Joined: May 17, 2005
Posts: 91
posted
0
In this case, you just call the method but don't try to embed the call in a print statememnt. The method handles the printing for you. So instead of this
you would do this:
Arul Prasad
Ranch Hand
Joined: Jan 20, 2005
Posts: 57
posted
0
hi kristin
The problem is you're calling the numberOfMovies() method in your print statement, and unless the numberOfMovies() method returns a string, you'll get the error you're seeing. It's okay if your runTest() method returns void.
for ur information numerOfMovies() can return any data type not only String it can return Float,Integer...etc, Object or any primitive data type println() method will work since this method inturn will call the Object.toString()
With Regards<br />Arul
Saeed Amer
Ranch Hand
Joined: Jan 20, 2004
Posts: 135
posted
0
Lisa, As suggested by Kristin - following line of code will print required message:
[/qb]<hr></blockquote>
You can only call a method within the "println" method if it is returning anything. The value being returned is then concatenated to the rest of the string (if any) in the "println" method.
For example, if your numberOfMovies() method was returning the count, your original line of code would have copiled with no errors.