-
Passing variables to a java function
Hey all, first post so be nice 
Ok, i want to pass some variables to a java function. They are an int variable, a string variable and an array. My function is as follows:
Code:
public static void makelist() {
if (list == null) {
list = count + " = ";
} // If this is the first item in the list do not add the list variable to the start because it is null
else {
list = list + count + " = ";
} // If it is not the first item then add the list variable
list = list + NameArray[count]; // Add the item in the array to the list
list = list + "\n"; // Add a new line to the list
count++; // Increment count
}
and part of the script where the function is called is below.
Code:
while (arraylength >= count)
{
makelist();
}
I want to pass the variables, NameArray, list and count. Any ideas?
Thanks.
Jack.
-
chenge your method descriptior to:
Code:
public static void makeList(int i, String s, String[] NameArray)
{}
This will enable you to pass said variables to the method.
One question though, why did you declare the method as static? Are you calling it from your main method?
Cause usually, methods are not static, but are called from within an instance of a class.
-
Hey, yeah im calling it from my main method, im not 100% what static means, im currently a student doing a BTEC in IT Practioning and thats all we've been taught so far, its a pretty basic course programming wise, which is why im using my initiative and trying to learn more. What would i be better putting it as instead of static and why?
Regards,
Jack.
P.S Ill change my code at college tomorow and post the results.
-
static methods are made so that they can be called from an external class without having to make an instance of that class.
For example, if you have a class called ClassA and it contains a method called sampleMethod() and you have declared it as static ie:
public static void sampleMethod() {}
that means that you can call this method from another class you create by typing the command:
ClassA.sampleMethod()
and it will run fine, if it was not static, you would have to do this:
ClassA a = new ClassA();
a.sampleMethod();
to call it.
Thats basically the "static" command in a nutshell
So just remove the word static completly and it will work fine inyour case.
A kram a day keeps the doctor......guessing
-
Sorry to do this guys but the function is now kind of working, I think the problem is it is not changing the variables outside of the function, part of my code is below, if you take a glance and see if anything springs out at you? Thanks in advance.
Jack.
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ItemCalcs {
public static void main ( String args[] ) {
String option;
option = JOptionPane.showInputDialog(
"Please enter your required option:\n\nCalculate Bill = 1\nRe-imburse Customer = 2\n\n");
if ( option.equals("1") ) { CalcBill(); }
else if ( option.equals("2") ) { CalcBill(); }
else { JOptionPane.showMessageDialog(null,
"Sorry but you selected an incorrect option!",
"Incorrect Option",
JOptionPane.INFORMATION_MESSAGE); }
}
public static void makeList(int count, String list, String[] NameArray) {
if (list == null) {
list = count + " = ";
} // If this is the first item in the list do not add the list variable to the start because it is null
else {
list = list + count + " = ";
} // If it is not the first item then add the list variable
list = list + NameArray[count]; // Add the item in the array to the list
list = list + "\n"; // Add a new line to the list
}
public static void CalcBill() {
{
String numItems, item, moneygiven, currencytotal, currencychange, list; // Define string variables
int numItemsInt, parseditem, i, count, arraylength, so1; // Define integer variables
double total, moneygivenparsed, change, deductamount; // Define double variables
String[] NameArray = new String[] {
"Large Coffee", "Small Coffee", "Large Tea", "Small Tea", "Teacake"}; // Define what elements are in the name array
double[] PriceArray = new double[] {
2.00, 1.50, 2.00, 1.00, 0.75}; // Define what elements are in the price array
total = 0; // Set total to zero
i = 0; // set i to zero
arraylength = NameArray.length; // Get array length
arraylength--; // Take 1 off the array becuase .length always returns 1 extra on top of the actual array length
numItems = JOptionPane.showInputDialog(
"Please enter the number of items the customer has bought\n\n"); // Gives list of item id's and allows input of items bought
numItemsInt = Integer.parseInt(numItems); // Parse the number of items customer has bought
if (numItemsInt != 0)
{
numItemsInt--; // Deduct 1 off number of items due to 0 being counted as an item
list = null; // Set list to null
count = 0; // Set count to zero
so1 = 0;
while (i <= numItemsInt)
{
//Calculate what items are in the array and automatically assign ID numbers
while (arraylength >= count)
{
makeList(count, list, NameArray);
count++; // Increment count
}
item = JOptionPane.showInputDialog(
"Enter Item Bought using the ID number from the list below: \n\n " +
list + "\n"); // User enters items bought in the form of ID numbers
-
Ok guys, i think ive found the problem, the variables are being trasmitted into the function, but as you can see above the list variable is used outside of the function, but it is not available, maybe i need to glabalise it or something along those lines?
Regards, Jack.
-
yeah make it a global variable, then all changes throughout your code will affect that variable, but can i suggest something? When you pass variables to a function, try not to call them the same names in the method name as they are in the method call. This just clarifies the use of different variables.
A kram a day keeps the doctor......guessing
-
Ok, ive looked at global variables in java and, to be honest im confused. What code would I need to be able to call this variable later on, the list is a progressive list which builds up and is then called, to be shown in a message box. So the variable is made is this code:
Code:
public static void makeList(int fcount, String flist, String[] fNameArray) {
if (flist == null) {
flist = fcount + " = ";
} // If this is the first item in the list do not add the list variable to the start because it is null
else {
flist = flist + fcount + " = ";
} // If it is not the first item then add the list variable
flist = flist + fNameArray[fcount]; // Add the item in the array to the list
flist = flist + "\n"; // Add a new line to the list
JOptionPane.showMessageDialog(null,
flist,
"ID Problem",
JOptionPane.INFORMATION_MESSAGE);
}
and then I need to use the newly made flist variable in this piece of code:
Code:
while (arraylength >= count)
{
makeList(count, list, NameArray); // Function where flist variable is made and needs to be outputed as the variable list
count++; // Increment count
}
item = JOptionPane.showInputDialog(
"Enter Item Bought using the ID number from the list below: \n\n " +
list + "\n"); // Where the list variable outputted from the function is used.
Within the function the variable is created fine but i cannot access it outside of the function.
Regards, Jack.
-
if the functionality of the list or flist variables needs to be something like this:
define
edit
display
edit
display
edit
display
etc...
then your order of operations would be to initiate the variable, then after the first set of editing is done and then it is outputted, you would then need to set it to empty again before your loop start again. Then the same process would apply over and over again. You get me?
Basically, you:
define list and set to ""
start loop
edit list with methods
display list
set list back to ""
loop again
A kram a day keeps the doctor......guessing
-
Ok, sorry guys, im not explaining myself properly here.
Basically all i want is to know how I access the variables that I have set inside the function, outside of the function.
So how do i use variables that have been set in my makeList function, in my CalcBill function.
Sorry if I have been too vague.
Thanks for all your help so far though, much appreciated!
Jack.
-
Ah, ok.
well there are several ways of doing this.
1: You can declare the variables as global. This way you will not have to pass them to the method, and every method will be able to acccess them. This is the easiest way, but not a very clean way.
2: You can create a second class, which will contain all the variables (A container). Now, when you pass this class to the method, you are actually passing a reference to this class (like a pointer in c++). Any change made to the variables in this container class is now accessable from every reference to that class, so it is also accessable in your calling method. (This procedure is analogous to pass-by-reference in C++)
-
Ok, ive searched about globalising variables, and I cannot find a simple tutorial that explains it? What code should I use to make the variables global?
Many thanks.
Jack.
-
I think in java they are called "instance variables" the way they work is simply just to define them out side of any method in your class. So just beneth the:
public class ItemCalcs {
you would declare some variables like so:
private String example = "ta daa";
now this string will be accessible by every variable in your entire class.
A kram a day keeps the doctor......guessing
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks