Results 1 to 9 of 9
  1. #1
    gabrielpr12 is offline Member
    Join Date
    Nov 2011
    Posts
    41
    Rep Power
    0

    Default Java Noob, trying to call methods from another methods

    I have been trying to write a code to call a method from another method, or chain them. After I declare a variable in the first method and try using it on the second method, but it keeps giving me the error "cannot find symbol". I have used the example on a Java book, but it gives me that error. How can I use a variable declared in a previous method?

  2. #2
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default Re: Java Noob, trying to call methods from another methods

    Variables declared in one method are local to that method and can't be used elsewhere. You'll have to pass it as an argument to that method.

    Since the variable and the parameter are both local, they can both be given the same identifier without causing any complaint from the compiler. This might be what's confusing you.

  3. #3
    gabrielpr12 is offline Member
    Join Date
    Nov 2011
    Posts
    41
    Rep Power
    0

    Default Re: Java Noob, trying to call methods from another methods

    Got it, but how can I use the result of a method in another. Let's say I create a method to calculate how many dozens of certain items, and now I want to use the result of that in another method within the same class, how do I do it, how do I pass that argument? I think that's my problem passing something from one method to another.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,305
    Blog Entries
    7
    Rep Power
    20

    Default Re: Java Noob, trying to call methods from another methods

    Quote Originally Posted by gabrielpr12 View Post
    Got it, but how can I use the result of a method in another. Let's say I create a method to calculate how many dozens of certain items, and now I want to use the result of that in another method within the same class, how do I do it, how do I pass that argument? I think that's my problem passing something from one method to another.
    The best way to pass something to a method is to pass it as a parameter; of course the method should expect such a parameter. e.g.

    Java Code:
    void foo() {
       int result= /* some complicated stuff */
       bar(result); // pass it to bar
    }
    void bar(int aValue) {
       ...
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    gabrielpr12 is offline Member
    Join Date
    Nov 2011
    Posts
    41
    Rep Power
    0

    Default Re: Java Noob, trying to call methods from another methods

    Thanks for the input, I will try it when I get to my computer.

  6. #6
    gabrielpr12 is offline Member
    Join Date
    Nov 2011
    Posts
    41
    Rep Power
    0

    Default Re: Java Noob, trying to call methods from another methods

    This is the code I am trying to use


    import java.util.Scanner;
    public class Eggs
    {
    public static int main(String[] args)
    {
    int numberOfEggs;
    Scanner inputDevice = new Scanner(System.in);
    System.out.print("Please enter total amount of eggs ");
    numberOfEggs = inputDevice.nextInt();
    numberOfDozens(numberOfEggs);
    }
    public static int numberOfDozens(int dozens)
    {
    int singleEggs;
    dozens = numberOfEggs / 12;
    singleEggs = numberOfEggs % 12;
    System.out.print("You have " + dozens + "dozens, and"
    + singleEggs + "single eggs");
    }
    }

    I am trying to tell the program to have a user input the number of eggs and then the program will calculate how many dozens and single eggs. I need to use two methods. Why can't I pass the result of the main method to the numberOfDozens. What I am doing wrong?

  7. #7
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default Re: Java Noob, trying to call methods from another methods

    The problem, as your compiler is probably telling you, is that numberOfDozens is supposed to return an int, but returns nothing. It should look more like:
    Java Code:
    class Foo {
        public int dozens(int num) {
            return num / 12;
        }
        public int modTwelve(int num) {
            return num % 12;
        }
        public void run() {
            int i = 100;
            System.out.println(i + " is " + dozens(i) + " dozen, with remainder " + modTwelve(i));
        }
        public static void main(String[] args) {
            Foo f = new Foo();
            f.run();
        }
    }
    Not very useful, but you get the idea.

    Alternatively, you could have dozens return an int of length 2, where the first element is the number of dozens, and the second is the remainder.

  8. #8
    gabrielpr12 is offline Member
    Join Date
    Nov 2011
    Posts
    41
    Rep Power
    0

    Default Re: Java Noob, trying to call methods from another methods

    I am new to all this, I don't know how to interpret what you put in the code. Can you explain it a little bit?

  9. #9
    javaTurtle is offline Member
    Join Date
    Nov 2011
    Location
    Arizona
    Posts
    10
    Rep Power
    0

    Default Re: Java Noob, trying to call methods from another methods

    Your creating a variable in ypur method: public static int numberOfDozens(int dozens). int dozens. I am assuming that int dozens is supposed to be the quantity of dozens that you have. If this is the case, look at your call from the MAIN : numberOfDozens(numberOfEggs); you are passing numberOfEggs. so, when you pass 25 eggs to numberOfDozens(25) you are setting the int dozens variable to 25. In the numberOfDozens method, you are changing that value (dozens = numberOfEggs / 12;)
    Your numberOfDozens() method needs to have the logic fixed.

Similar Threads

  1. how to call or use methods inside interface?
    By akhmad in forum New To Java
    Replies: 2
    Last Post: 11-10-2010, 09:55 AM
  2. Call Static Methods Outside Classes
    By Insomniac Riot in forum New To Java
    Replies: 4
    Last Post: 05-11-2010, 10:03 PM
  3. Call java Methods from Python Script
    By hofsoc in forum New To Java
    Replies: 1
    Last Post: 02-18-2009, 04:47 PM
  4. How to call methods of different classes
    By adeeb in forum New To Java
    Replies: 2
    Last Post: 06-06-2008, 06:08 AM
  5. Replies: 0
    Last Post: 01-02-2008, 01:52 PM

Posting Permissions

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