Results 1 to 9 of 9
- 11-17-2011, 10:36 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 41
- Rep Power
- 0
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?
- 11-17-2011, 10:51 AM #2
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
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.
- 11-17-2011, 11:16 AM #3
Member
- Join Date
- Nov 2011
- Posts
- 41
- Rep Power
- 0
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.
- 11-17-2011, 12:08 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
Re: Java Noob, trying to call methods from another methods
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.
kind regards,Java Code:void foo() { int result= /* some complicated stuff */ bar(result); // pass it to bar } void bar(int aValue) { ... }
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-17-2011, 04:04 PM #5
Member
- Join Date
- Nov 2011
- Posts
- 41
- Rep Power
- 0
Re: Java Noob, trying to call methods from another methods
Thanks for the input, I will try it when I get to my computer.
- 11-17-2011, 07:09 PM #6
Member
- Join Date
- Nov 2011
- Posts
- 41
- Rep Power
- 0
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?
- 11-17-2011, 07:37 PM #7
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
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:
Not very useful, but you get the idea.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(); } }
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.
- 11-17-2011, 08:05 PM #8
Member
- Join Date
- Nov 2011
- Posts
- 41
- Rep Power
- 0
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?
- 11-17-2011, 09:07 PM #9
Member
- Join Date
- Nov 2011
- Location
- Arizona
- Posts
- 10
- Rep Power
- 0
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
-
how to call or use methods inside interface?
By akhmad in forum New To JavaReplies: 2Last Post: 11-10-2010, 09:55 AM -
Call Static Methods Outside Classes
By Insomniac Riot in forum New To JavaReplies: 4Last Post: 05-11-2010, 10:03 PM -
Call java Methods from Python Script
By hofsoc in forum New To JavaReplies: 1Last Post: 02-18-2009, 04:47 PM -
How to call methods of different classes
By adeeb in forum New To JavaReplies: 2Last Post: 06-06-2008, 06:08 AM -
How can I call java class methods in Oracle 10g PL/SQL
By searcher34 in forum JDBCReplies: 0Last Post: 01-02-2008, 01:52 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks