Page 1 of 2 12 LastLast
Results 1 to 20 of 28
Like Tree1Likes

Thread: Add doubles in an array

  1. #1
    maxiffer is offline Member
    Join Date
    Dec 2013
    Posts
    14
    Rep Power
    0

    Default Add doubles in an array

    Hey guys I have a mini project for computer science and I'm a bit stuck right now.
    I'm allowing the user to choose certain items to buy that is moved to an array.
    Now I'm trying to add those thing in the array use a different class.
    I don't understand how I can call the array from my driver class to my checkout class that adds them together.

  2. #2
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    177
    Rep Power
    2

    Default Re: Add doubles in an array

    Can you post an example of your code please
    With the right know how, anything is possible

  3. #3
    maxiffer is offline Member
    Join Date
    Dec 2013
    Posts
    14
    Rep Power
    0

    Default Re: Add doubles in an array

    This is in the checkout class, and it won't compile because the finalTotal array is in my driver class. :[

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

  4. #4
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    177
    Rep Power
    2

    Default Re: Add doubles in an array

    you could make the checkout class extend the driver class
    maxiffer likes this.
    With the right know how, anything is possible

  5. #5
    maxiffer is offline Member
    Join Date
    Dec 2013
    Posts
    14
    Rep Power
    0

    Default Re: Add doubles in an array

    It's already implementing my interface class though
    never mind ignore this... I'm dumb

  6. #6
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    177
    Rep Power
    2

    Default Re: Add doubles in an array

    lol did it work
    With the right know how, anything is possible

  7. #7
    maxiffer is offline Member
    Join Date
    Dec 2013
    Posts
    14
    Rep Power
    0

    Default Re: Add doubles in an array

    I made it extend from my driver class but it says it cant find the varaiable finalTotal still

  8. #8
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    177
    Rep Power
    2

    Default Re: Add doubles in an array

    can you please wrap it in code tags. Its the button with the # sign
    With the right know how, anything is possible

  9. #9
    maxiffer is offline Member
    Join Date
    Dec 2013
    Posts
    14
    Rep Power
    0

    Default Re: Add doubles in an array

    Java Code:
    public class Checkout extends restaurantDriver implements Restaurant 
    {
        private double sum;
        public Checkout()
        {
            sum = 0;
        }//end Checkout()
        
        public double getTotal()
        {
            for (int i; i< finalTotal.length; i++)
            {
             sum + finalTotal[i] = sum;
            }
            return sum;
        }
    }//end class Checkout
    This is my whole checkout class
    It still can't find the finalTotal array

  10. #10
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,411
    Rep Power
    5

    Default Re: Add doubles in an array

    Perhaps it either doesn't exist in your restaurantDriver class, it is a typo, or it is declared private. And class names start with uppercase by convention.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  11. #11
    maxiffer is offline Member
    Join Date
    Dec 2013
    Posts
    14
    Rep Power
    0

    Default Re: Add doubles in an array

    Quote Originally Posted by jim829 View Post
    Perhaps it either doesn't exist in your restaurantDriver class, it is a typo, or it is declared private. And class names start with uppercase by convention.

    Regards,
    Jim

    I just checked and it spelt correctly and it is in my driver class.

    This is the array in my driver class, now I'm confused on why it's not working D:
    double[] finalTotal = new double[10];

  12. #12
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,411
    Rep Power
    5

    Default Re: Add doubles in an array

    You need to show your class. Is it an instance field or declared within main (which won't work)?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  13. #13
    maxiffer is offline Member
    Join Date
    Dec 2013
    Posts
    14
    Rep Power
    0

    Default Re: Add doubles in an array

    Quote Originally Posted by jim829 View Post
    You need to show your class. Is it an instance field or declared within main (which won't work)?

    Regards,
    Jim
    I don't want to post the whole class cause it a lot of lines.

    The only use of my "finalArray" is to hold the price of what the user wants to "purchase".
    I only have this line that creates my array.
    double[] finalTotal = new double[10];
    Everything else in my Driver is taking input from the user and moving the price from one array to finalArray.
    And this is where I ran into the problem of where my checkout class not being able to find the array finalArray.

  14. #14
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,411
    Rep Power
    5

    Default Re: Add doubles in an array

    Is it finalArray or finalTotal? And where is the array declared? You need to at least show that. Is it in within a method or is it an instance field?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  15. #15
    maxiffer is offline Member
    Join Date
    Dec 2013
    Posts
    14
    Rep Power
    0

    Default Re: Add doubles in an array

    Sorry my bad its a finalTotal array.
    I'm not really sure what that means, sorry still really new to this.
    Its declared in the method main of my driver class

    Java Code:
    public static void main (String args[]) throws IOException
    {
      Checkout total = new Checkout();
    
      double[] finalTotal = new double[10];
    }//end method main
    I cut out a lot of lines but everything else is just different arrays

  16. #16
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,411
    Rep Power
    5

    Default Re: Add doubles in an array

    That's your problem right there. You have declared the array inside of main which makes it a local variable to that method. Move it above the main method just inside the class declaration. Then it will be visible to your subclass.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  17. #17
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Add doubles in an array

    This applies with everything. In order for a variable to be accessed from outside the class, it has to be global and it can't be private.

  18. #18
    maxiffer is offline Member
    Join Date
    Dec 2013
    Posts
    14
    Rep Power
    0

    Default Re: Add doubles in an array

    Quote Originally Posted by jim829 View Post
    That's your problem right there. You have declared the array inside of main which makes it a local variable to that method. Move it above the main method just inside the class declaration. Then it will be visible to your subclass.

    Regards,
    Jim
    It says a non static variable can't reference a static one, since I'm using an array with prices in them to move it to the finalTotal array
    So wouldn't the array have to be in the method main?

    Java Code:
     while (ctr < dPrice.length)
          {
              if(drink.equalsIgnoreCase("water"))
              {
                finalTotal[ctr] = dPrice[0];
                break;
              }

  19. #19
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Add doubles in an array

    Quote Originally Posted by maxiffer View Post
    It says a non static variable can't reference a static one, since I'm using an array with prices in them to move it to the finalTotal array
    So wouldn't the array have to be in the method main?

    Java Code:
     while (ctr < dPrice.length)
          {
              if(drink.equalsIgnoreCase("water"))
              {
                finalTotal[ctr] = dPrice[0];
                break;
              }
    No, the finalTotal array needs to have a static access modifier.

  20. #20
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,411
    Rep Power
    5

    Default Re: Add doubles in an array

    I understand the problem you are having. There are two possible solutions. The first is to do one and only one thing in your main class. And that is this:
    Java Code:
    new ClassName().start();
    Then you just need to declare a non-static method called start where you do all of your processing. You can then access instance fields unfettered. The other option is more cumbersome.
    Java Code:
    ClassName cl = new ClassName();
    Then if you want to access any instance fields inside of main you need to prepend the instance qualifier, like this:
    Java Code:
    cl.myVariable = 23; // or whatever (assuming it is a number)
    If you want to access instance fields from other static methods you need to pass an instance. Which is usually why I steer away form static methods.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 3
    Last Post: 12-20-2013, 08:17 PM
  2. Help adding doubles to an array
    By HalfAZN in forum New To Java
    Replies: 2
    Last Post: 10-18-2012, 08:47 PM
  3. Replies: 3
    Last Post: 06-18-2012, 09:44 AM
  4. Arithmetic using doubles
    By Black_Eye in forum New To Java
    Replies: 4
    Last Post: 10-19-2010, 08:35 AM
  5. Cant use doubles
    By Freakzoyd in forum New To Java
    Replies: 27
    Last Post: 07-24-2010, 04:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •