aspose file tools*
The moose likes Java in General and the fly likes Void type not allowed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Void type not allowed" Watch "Void type not allowed" New topic
Author

Void type not allowed

Lisa Beglaw
Ranch Hand

Joined: Jul 16, 2005
Posts: 65

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
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
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
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
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
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
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
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: 140
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.

Saeed
[ August 10, 2005: Message edited by: Saeed Amer ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41821
    
  62
Something like the following should do:

System.out.print("The MovieCollection has ");
movieCollection.numberOfMovies();
System.out.println(" movies.");


Ping & DNS - my free Android networking tools app
Sathya Srinivasan
Ranch Hand

Joined: Jan 29, 2002
Posts: 379
As per your code, the best approach would be



Since your code really is updating the number of movies, you might want to change your method name to updateMovieCollection() to cause less confusion.


Cheers, Sathya Srinivasan - SCJP 1.2, SCWCD 1.2, SCMAD 1.0
Co-Author of Whizlabs SCMAD Certification Exam Simulator and SCMAD Exam Guide Book
Lisa Beglaw
Ranch Hand

Joined: Jul 16, 2005
Posts: 65
Everyone, Thank you!

As suggested, I will use Kristin's advice. Thanks Kristin!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Void type not allowed