0

Hello I have a project for my class and I'm a bit stuck right now. I created a custom class with this code:

public double getTotal()
{
    for (int i; i< finalTotal.length; i++)
    {
     sum + finalTotal[i] = sum;
    }
    return sum;
}//end getTotal()

The array finalTotal is created in my driver class in the method main, but my custom class that suppose to find the total of the array but, won't compile because it can't find the variable finalTotal. So how would I allow my custom class to be able to access info from the array in my driver class?

public static void main (String args[]) throws IOException
{
 Checkout total = new Checkout();
 double finalTotal[] = new double[10];
}//end method main
5
  • We need some more code out there. Where is finalTotal defined? Is it inside main method? If yes, you can't access it in any other method, b'coz that is local to main method. For more help, please post more code. Commented Jan 20, 2014 at 16:49
  • 1
    Post more code. Where is finalTotal declared? Impossible to answer without more information. Commented Jan 20, 2014 at 16:50
  • 2
    Apart from your compile error, you probably meant: sum = sum + finalTotal[i]; (the part on the right of = is allocated to the part on the left of =). Commented Jan 20, 2014 at 16:50
  • What is the visibility of the finalTotal array? Also, shouldn't you just call getTotal()? And, sum += finalTotal[i];. Commented Jan 20, 2014 at 16:51
  • Ok Sorry I'm updating my post right now. But yes the finalTotal array is in my driver inside the method main. Commented Jan 20, 2014 at 16:54

3 Answers 3

1

I think you wanted to write

sum = finalTotal[i] + sum;

instead of

sum + finalTotal[i] = sum;

And you have to pass your array in parameter of your method. So the could would look like :

public double getTotal(double[] finalTotal) {
    for (int i; i < finalTotal.length; i++) {
        sum = finalTotal[i] + sum;
    }
    return sum;
}
Sign up to request clarification or add additional context in comments.

2 Comments

he probably also needs to pass the array as an argument of the method from his main class.
@assylias and @ csmckelvey : Fixed !
0

Assuming your finalTotal variable is in another class, you need to design the method and maybe class you provided above to take in a reference to this variable. For instance, the constructor could be written thusly:

public MyClass (int[] finalTotal)
{
    //code here
}

If you design a second class, it has no idea what is in the first class unless you pass it the information that it needs!

Comments

0

If a got the idea this is not a class.. this is a method called getTotal();

If you want to access the array, you need to pass it through param method, as:

public double getTotal(double[] finalTotal) //or whatever the type of the array is
{
    for (int i; i< finalTotal.length; i++)
    {
         sum += finalTotal[i] ;
    }
    return sum;
}

Hope it helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.