We help IT Professionals succeed at work.
Get Started

changing my code to add a loop to put in values

jerry-2112
jerry-2112 asked
on
786 Views
Last Modified: 2012-08-24
I have a code here that works fine, but I was wondering how to change it to use a loop to put in my array vaules.  This was fairly easy todo because there was only 20 total vaules to add but larger program would need some sort of loop.  Here is my working Code
import java.util.ArrayList;

public class ThreeArrayLists {

    public static void main(String[] args) {
        ArrayList priceList = new ArrayList();
        ArrayList quantityList = new ArrayList();
        ArrayList amountList = new ArrayList();
        //PRICE lIST
        priceList.add(new Double(10.62));
        priceList.add(new Double(14.89));
        priceList.add(new Double(13.21));
        priceList.add(new Double(16.55));
        priceList.add(new Double(18.62));
        priceList.add(new Double(9.47));
        priceList.add(new Double(6.58));
        priceList.add(new Double(18.32));
        priceList.add(new Double(12.15));
        priceList.add(new Double(3.98));
        //QUANTITY LIST   
        quantityList.add(new Double(4.0));
        quantityList.add(new Double(8.5));
        quantityList.add(new Double(6.0));
        quantityList.add(new Double(7.35));
        quantityList.add(new Double(9.0));
        quantityList.add(new Double(15.3));
        quantityList.add(new Double(3));
        quantityList.add(new Double(5.4));
        quantityList.add(new Double(2.9));
        quantityList.add(new Double(4.8));
       // call extend method
        extend(amountList, quantityList, priceList);
        //call display method
        display(quantityList, priceList, amountList);






    }
// display method
    private static void display(ArrayList quantityList, ArrayList priceList, ArrayList amountList)  {
          //for loop to display arrays
        for (int i = 0; i < 10; i++) {
        //format using %.2f to round all numbers to 2 decimals
            System.out.format("%d)  %.2f * %.2f = %.2f \n",i+1, 
Double.valueOf(priceList.get(i).toString()), 
Double.valueOf(quantityList.get(i).toString()), Double.valueOf(amountList.get(i).toString()));
        }
    }

    private static void extend(ArrayList amountList, ArrayList quantityList, ArrayList priceList) {
           //for loop to do the math
        for (int i = 0; i < 10; i++) {
            amountList.add((Double.valueOf(priceList.get(i).toString())) * (Double.valueOf(quantityList.get(i).toString())));



        }

    }
}

Open in new window

Here is the sort of loop I'd like to add.

for i = 0 to i < 10
{
priceList.add(priceArray[i]);
quantityList.add(quantityArray[i]);
}

Open in new window

So would my Array totals look like this :
final double[] PRICE_ARRAY = { 10.62, 14.89, 13.21, 16.55, 18.62, 9.47,
            6.58, 18.32, 12.15, 3.98 };
    final double[] QUANTITY_ARRAY = { 4.0, 8.5, 6.0, 7.35, 9.0, 15.3, 3.0, 5.4,
            2.9, 4.8 };

Open in new window

Is this a simple change or hardcore ?  What I have is good for my assignment, just wondering.  Thanks Jerry
Comment
Watch Question
IT Business Systems Analyst / Software Developer
CERTIFIED EXPERT
Top Expert 2015
Commented:
This problem has been solved!
Unlock 4 Answers and 7 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE