Results 1 to 6 of 6
Like Tree2Likes
  • 1 Post By jim829
  • 1 Post By DarrylBurke

Thread: how to print results from void method into main method?

  1. #1
    BookOfEliX is offline Member
    Join Date
    Feb 2015
    Posts
    20
    Rep Power
    0

    Default how to print results from void method into main method?

    When I try to print out the results from a void method in the main method i get the following error message "method printFib in class testFibonacci cannot be applied to given types;". Can anyone help me?

    Java Code:
    import java.util.*;
    public class testFibonacci{
       public static void main(String[] args){
       //obtain limit from user.
       Scanner input = new Scanner(System.in);
       //int input = 0;
       System.out.println("Please enter limit: ");
       int limit = input.nextInt();
       printFib(limit);
       System.out.println(printFib());
       
       }
       
       public static void printFib(int limit){
          int firstPrevious = 0;
          int secondPrevious = 1; 
          int sum = firstPrevious + secondPrevious; 
          do{
            sum = firstPrevious + secondPrevious;
            secondPrevious = sum;
            firstPrevious = secondPrevious;
          }while(sum <= limit);    
       }  
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: how to print results from void method into main method?

    You should read the tutorial about how to define and call methods:
    Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    Passing Information to a Method or a Constructor (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    What is the purpose of the printFib() method? I don't see any comments in the code describing what it is supposed to do.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    878
    Rep Power
    12

    Default Re: how to print results from void method into main method?

    printfb() is not returning anything.How can u print?
    Ramya:cool:

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: how to print results from void method into main method?

    printfb() is not returning anything
    Then its definition should be changed so that it does return something.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: how to print results from void method into main method?

    A note on coding in general. Give your methods the proper names. printFib() doesn't print anything and I doubt it is supposed to. My guess would be a more accurate name is findNthFibonacci number. Or findNthFib. Or computeNthFib.

    Regards,
    Jim
    Norm likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: how to print results from void method into main method?

    Quote Originally Posted by jim829 View Post
    printFib() doesn't print anything
    But that's what makes it a fib
    jim829 likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. how to assert a void method?
    By jrdn128 in forum Advanced Java
    Replies: 5
    Last Post: 11-05-2014, 11:59 AM
  2. Replies: 4
    Last Post: 03-28-2013, 08:40 PM
  3. looking for recursive help using Void method
    By tripline in forum New To Java
    Replies: 10
    Last Post: 10-27-2011, 06:48 PM
  4. Replies: 2
    Last Post: 09-27-2011, 03:59 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •