can someone explain to me how i can pass the array "test" and the int "total" back to the main by reference.
public static void CalculateAverage() throws IOException { int[] test=new int[7]; int count; int total=0; for (count = 0;count < 5; count++) { test[count] = Integer.parseInt(tokenizer.nextToken()); total = test[count] + total; } }
the " int total" can only be passed by value, not reference, like all primivitive types of java, however, if you really wanted to bypass this you could declare "static int total" as a globabl variable, but this can tend to be bad practice. The test array can be passed through reference, but you might want to change the scope of it and put it outside of the method maybe in main, and then pass the array to the CalculateAverage() method. First off, a tip for java is that your "main" method should probably only consist of one or two lines of code. Let me give you an example of how i would do something like this, I will keep main simple, pass the array by reference, and the int by value.
class Example {
public static void main (String []args)
{
Example p = new Example ();
p.getStarted;
//You could also do "this.getStarted "
}
public void getStarted()
{
int [] test = new int [7];
int total = CalculateAverage(total, test);
}
public int CalculateAverage(int total, int test []) throws IOException
{
for (x = 0;x < 5; xt++)
{
test[x] = Integer.parseInt(tokenizer.nextToken());
total = test[x] + total;
}
return total;
}
I hope this helps.