Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Help with double array method

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Help with double array method

    Hi,
    I am new to programming. I was asked to make a program that uses a method to sum the grandtotal and a method for sorting.

    So far the output of the program is perfect, however, i have not met the requirement of having a method sum the totals and a method to sort the collections. I have wrecked my brain trying to figure this out for two days. I will post my code and any help will be appreciated. By the way I have also tried building arrays to hold the (price*cases) amount, but there is no method to add a double to a regular array.

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package farmersmarket2;
     
    import java.lang.reflect.Array;
    import java.util.Scanner; //uses scanner
    import java.text.NumberFormat;//needed to format currency
    import java.util.Locale; //used to get local format for currency
    import java.util.ArrayList;//to create arrays
    import java.util.Arrays;//to sort array
    import java.util.Collections;//sort arraylist sisnce arrays will not sort lists
     
    /**
     *
     * @author David
     */
    public class FarmersMarket2 {
     
        /**
         * @param args the command line arguments
         */
     
     
     
        public static void main(String[] args) {
     
            Scanner input = new Scanner(System.in); // new scanner named input
            NumberFormat format = NumberFormat.getCurrencyInstance(Locale.US);
            ReceiptBuilder2 RB2; //Storage object
     
     
            //Declare Variables
            String Produce;//variable for produce name
            String produce;//store lower case name to change to upper
            String SKU ;//will hold SKU of 8 digits
            int cases; //will hold number of cases ordered
            double price;//price of produce
            boolean moreItems = true; //while control variable
            double ggtt = 0;//holds grandtotal
     
     
            //create Arrays
        ArrayList<String> receipt = new ArrayList<>();//10 element array
       //this is where i tried to create an array double[] runtotal 
     
     
            while (moreItems){ 
            //Ask for produce name
          System.out.println("Enter produce name to continue or"
                  +"\"end\" to get total: ");
                 produce = input.next();//set variable with name input
               Produce = produce.toUpperCase();
     
     
                 //test for name to change moreItems
           if (!Produce.equalsIgnoreCase("end"))      
           {
     
     
     
           System.out.println(); //blank line for readability
     
            // Ask for 8 digit minimum SKU
            System.out.println("Please enter the 8 digit SKU number: ");//Get SKU
                SKU = input.next();//set variable SKU
            //validation of input    
            while (SKU.length() != 8 ) // check lenght of SKU    
            {     
             System.out.println("SKU must be exactly 8 digits. Please re-enter: ");
             SKU = input.next(); 
            }  // end while 
     
             System.out.println(); //Blank line for readibility
     
     
     
            //Ask for case quantity
            System.out.println("How many cases of this produce do you want? ");
               cases = input.nextInt();//set cases variable
     
            System.out.println();//blank line
     
     
     
            //Ask for produce price
            System.out.println("What is the price of each case? ");
                price = input.nextDouble();//set variable price
     
     
     
     
     
     
     
           //line break
         System.out.println("\n\n******************************************\n\n");
     
         //change the ggtt variable to a running total
     
         ggtt = ggtt+(price*cases);
        //at this point i would need to add the total to the array
         //so far failed miserably
     
     
       //send all info to new constructor
           //new constructor with 4 variables passed to it 
     
        RB2 = new ReceiptBuilder2(Produce,SKU,cases,price);
     
     
     
     
     
        receipt.add(RB2.toString());//add strings to the receipt array
     
     
     
        //Print receipt
            System.out.println("Thanks for shopping at the Farmer's Market!!\n");
            System.out.print("Product: "+ RB2.getName()+"\n");
            System.out.print("SKU number: "+RB2.getBarcode()+"\n");
            System.out.print("Price per case: "+format.format(RB2.getCost())+"\n");
            System.out.print("Cases ordered: "+RB2.getAmount()+"\n");
           System.out.print("SubTotal price: "+format.format(RB2.getTotal()) +"\n");
     
             //line break
         System.out.println("\n\n******************************************\n\n");
     
     
           }//end if portion
           else//begin else portion of if
           {
           moreItems = false;
     
     
     
           System.out.println("\n\n******************************************\n\n"
                   + "\n\nThanks for shopping at the Farmer's Market!!\n");
     
     
           //display elements in array receipt
           System.out.println("Unsorted version of receipt");
     
     
         for (String item : receipt) {//display items in array receipt
             System.out.printf("%s\n\n", item);
             }//end for
     
     
         try
         {
     
         System.out.print("Order Total: "+format.format(ggtt));
         }
         catch (NullPointerException nullPointerException )
         {
             System.out.print("You did not purchase any items.\nTotal: $0.00");
         }    
     
     
     
                    //line break
         System.out.println("\n\n******************************************\n\n"
                 + "\n\nThanks for shopping at the Farmer's Market!!\n");
     
     
     
     
           }//end else area
     
             }//end while
     
          //method to sort arraylist
     
           Collections.sort(receipt);//method to sort list
     
           System.out.println("Sorted version of the reciept.");
     
            for (String temp2 : receipt)
            {
                System.out.print(temp2+"\n\n");
            }
            System.out.print("Order Total: "+format.format(ggtt));//sho total again
            System.out.println("\n\n*************\n\n");
     
     
     //here I would like to print out a test to be sure that the sum of the array 
            //is correct before I alter my program.
     
     
        }//end main
     
     
        //I had created the method to sum the array here but it did not work either
     
     
     
        }//end class
    Last edited by Dsr122076; November 16th, 2012 at 06:23 AM. Reason: following suggestion below.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with double array method

    Please Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    a method sum the totals
    Where are the totals that you want to sum? If they are in an array, the normal way to sum the contents of an array is to use a loop that iterates over the contents of the array indexing one element each time through the loop.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with double array method

    Quote Originally Posted by Norm View Post
    Please Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.


    Where are the totals that you want to sum? If they are in an array, the normal way to sum the contents of an array is to use a loop that iterates over the contents of the array indexing one element each time through the loop.
    that is the problem. The sums are already summed in the ggtt amount. The input comes from the user input in the first if section. I can hold the running total in that variable. However, the assignment is to create a method to sum up the total. I tried creating an array as follows: double[] runtotal;

    however, since the input comes from the user there is no way to add this to the array. when I try runtotal.add(cases*amount); it states that it cannot add doubles because there is no method to do so. The amount I need to store in the array must be a double because it is far more accurate than a float.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with double array method

    way to add this to the array
    The normal way to get user input and add it to an array is to use a loop. Inside the loop ask the user for the input, check for valid data and then store the data in an element of the array that is indexed by a variable. There needs to be a way for the user to exit the loop.

    there is no method to do so
    Can you change the existing method or write a new one that takes the data type you want to use?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with double array method

    I am restarting this program in a different way since I hit a hard wall. I thought about adding to the array with a string and then parsing it into a string.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with double array method

    Post the code and ask questions about the problems you are having.
    I have no idea what your last post means.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How can I store a double array in mysql
    By luck999 in forum JDBC & Databases
    Replies: 1
    Last Post: March 15th, 2012, 05:42 AM
  2. Elements in double array (java)
    By wrx12 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: March 9th, 2012, 11:23 PM
  3. [SOLVED] Help with method returning a double
    By Mike_Chase in forum What's Wrong With My Code?
    Replies: 9
    Last Post: July 22nd, 2011, 01:09 AM
  4. [SOLVED] Read double from console without having to read a string and converting it to double.
    By Lord Voldemort in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: June 26th, 2011, 08:08 AM
  5. Error using public static double Method
    By stommy989 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 13th, 2010, 03:01 PM

Tags for this Thread