473,399 Members | 3,106 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

how do I return/pass variables from methods?

I am running Mac OSX 10.4.7

I am building a program that tracks the Total Sales for employees

Here is my code, it compiles fine but how do I take a variable from a method and pass it to another method?

Expand|Select|Wrap|Line Numbers
  1. public class main_Menu
  2. {
  3.    public static void main (String[] args)
  4.    {
  5.         main_menu();
  6.    }
  7.  
  8.    public static void main_menu ()
  9.    {
  10.      System.out.println (" ");
  11.      System.out.println ("Please Choose One of the Following (chose a #)");
  12.  
  13.         System.out.println ("1. Enter Data for employee");
  14.         System.out.println ("2. Show total sales");
  15.         System.out.println ("3. Show average sale");
  16.         System.out.println ("4. Show max and min sale");
  17.         System.out.println ("5. Check for sales larger then");
  18.  
  19.         int user_Choice = 0; 
  20.  
  21.         try
  22.         {
  23.  
  24.             BufferedReader in = new BufferedReader( new InputStreamReader(System.in));
  25.             user_Choice =  Integer.parseInt( in.readLine() );            
  26.         }
  27.  
  28.         catch ( Exception e )
  29.         {
  30.             e.printStackTrace();
  31.         }
  32.  
  33.         if (user_Choice == 1){
  34.         enter_Data();
  35.         }
  36.  
  37.         else if (user_Choice == 2){
  38.         total_Sales();
  39.         }
  40.  
  41.         else if (user_Choice == 3){
  42.         average_Sales();
  43.         }
  44.  
  45.         else if (user_Choice == 4){
  46.         max_min_sale ();
  47.         }
  48.  
  49.         else if (user_Choice == 5){
  50.         check_sales_largerthan ();  
  51.         }
  52.    }
  53.  
  54.    public static void enter_Data ()
  55.    {
  56.      int sale [] = {0,0,0,0,0};
  57.      String name [] = {" ", " ", " ", " ", " "};
  58.  
  59.      System.out.println ("Enter Employee Name, than sale");
  60.  
  61.      try
  62.         {
  63.             BufferedReader in = new BufferedReader( new InputStreamReader(System.in));
  64.             name[0] = in.readLine();
  65.             sale [0] = Integer.parseInt( in.readLine() );
  66.  
  67.             name[1] = in.readLine();
  68.             sale [1] = Integer.parseInt( in.readLine() );
  69.  
  70.             name[2] = in.readLine();
  71.             sale [2] = Integer.parseInt( in.readLine() );
  72.  
  73.             name[3] = in.readLine();
  74.             sale [3] = Integer.parseInt( in.readLine() );
  75.  
  76.             name[4] = in.readLine();
  77.             sale [4] = Integer.parseInt( in.readLine() );
  78.      }
  79.  
  80.      catch ( Exception e )
  81.         {
  82.             e.printStackTrace();
  83.         }
  84.  
  85.      System.out.println (name[0] + ": " + sale [0]);
  86.      System.out.println (name[1] + ": " + sale [1]);
  87.      System.out.println (name[2] + ": " + sale [2]);
  88.      System.out.println (name[3] + ": " + sale [3]);
  89.      System.out.println (name[4] + ": " + sale [4]);
  90.      main_menu();
  91.    }
  92.  
  93.  
  94.    public static void total_Sales ()
  95.    {
  96.      System.out.println (sale [0] + sale [1] + sale [2] + sale [3] + sale [4]);
  97.    }
  98.  
  99.    public static void average_Sales()
  100.    {
  101.      int average = (sale [0] + sale[1] + sale[2] + sale[3] + sale[4])/5;
  102.      System.out.println (average);
  103.    }
  104.  
  105. }
  106.  
For example I want to print the total sales, but the variables needed for that are in a different method. How do I take those variables and pass them into "total_Sales"?

any help would be appreciated.
Mar 9 '07 #1
1 1705
sicarie
4,677 Expert Mod 4TB
You have everything declared as void. Declare the method to be of the type you want to return. Then return an object of that type inside. This does mean that where you call it from, you will need to have an object of that type to take the value.

(And you could always declare the variables outside of main - that would give their scope to the entire program, and they could be called from other methods without being passed.)
Mar 9 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

27
by: Maximus | last post by:
Hi, I was just wondering, is it good to use return without arguments in a void function as following: void SetMapLayer() { if( !Map ) return; layer = LAYER_MAP; }
8
by: Ravindranath Gummadidala | last post by:
Hi All: I am trying to understand the C function call mechanism. Please bear with me as I state what I know: "every invocation of a function causes a frame for that function to be pushed on...
20
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt =...
14
by: Derek Basch | last post by:
This one has always bugged me. Is it better to just slap a "self" in front of any variable that will be used by more than one class method or should I pass around variable between the methods? ...
8
by: WakeBdr | last post by:
I'm writing a class that will query a database for some data and return the result to the caller. I need to be able to return the result of the query in several different ways: list, xml,...
9
by: Tyler | last post by:
Hello All: I am currently working on a project to create an FEM model for school. I was thinking about using wxPython to gather the 12 input variables from the user, then, after pressing the...
11
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible...
12
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.