• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 769
  • Last Modified:

changing my code to add a loop to put in values

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
0
jerry-2112
Asked:
jerry-2112
  • 2
  • 2
  • 2
  • +1
4 Solutions
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Your array initialisation looks fine. And your loop is almost there, try this...

for (int i = 0; i < PRICE_ARRAY.length; i++)
{
  priceList.add(PRICE_ARRAY[i]);
  quantityList.add(QUANTITY_ARRAY[i]);
}

Open in new window

Just make sure that there is always the same number (or more) entries in quantityArray than in priceArray, or else you will get an exception. To make sure this doesn't happen, the next step is to use a multi-dimensional array, such as...

final double[][] DETAILS_ARRAY = { 
  { 10.62, 4.0 },
  { 14.89, 8.5},
  { 13.32, 6.0},
};

for (int i = 0; i < DETAILS_ARRAY.length; i++) {
  priceList.add(DETAILS_ARRAY[i][0]);
  quantityList.add(DETAILS_ARRAY[i][1]);
}

Open in new window

And then, you would really look to make a Custom class where objects of that class represents each item of data (ie, an items price, quantity and amount), but that is for another day! ;)
0
 
CEHJCommented:
Just make sure that there is always the same number (or more) entries in quantityArray than in priceArray, or else you will get an exception. To make sure this doesn't happen, the next step is to use a multi-dimensional array, such as...

You need to make your code more object oriented really. Using that kind of approach might be fine for first steps in learning C (later there even you'd use a struct) but not really for Java. Java is an object oriented language and should be used as such from the outset.

What you need is a class called, say 'Item' that encapsulates the attributes of 'price', 'quantity' etc. You would create instances of Item and manipulate them accordingly. Synchronization of List length problems then disappear, though of course that's not the principal motivation.
0
 
junction-zeroCommented:
Don't reinvent the wheel, but stand on the shoulders of giants.
Use the library to do the dirty work of looping over the array to create a list and just call:
List list = Arrays.asList(array);
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
jerry-2112Author Commented:
Ok guys. I'm missing something ! Please don't give up on me.  
import java.util.ArrayList;

public class ThreeArrayLists1 
{
   
	// declare instance variables, arrays
    private ArrayList<Double> priceList = null;
    private ArrayList<Double> quantityList = null;
    private ArrayList<Double> amountList = null;

    final double[][] DETAILS_ARRAY = { 
    		  { 10.62, 4.0 },
    		  { 14.89, 8.5},
    		  { 13.32, 6.0},
    		  { 16.55,7.35 },
    		  { 18.62, 9.0},
    		  { 9.47, 15.3},
    		  { 6.58, 3.0 },
    		  { 18.32, 5.4},
    		  { 12.15, 2.9},
    		  { 3.98, 4.8 }
    		};


    public ThreeArrayLists1() 
    {
        // call the extend method
        extend();
    }

    public void extend(priceList, quantityList) 
    {
    	 for (int i = 0; i < DETAILS_ARRAY.length; i++) {
       	  priceList.add(DETAILS_ARRAY[i][0]);
       	  quantityList.add(DETAILS_ARRAY[i][1]);
        
        //call the display method
        display();
        
    } // end extend 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()));
           }
       }

    public static void main(String[] args) 
    {
        new ThreeArrayLists1();
    }
} 

Open in new window

 I'm trying !
0
 
CEHJCommented:
Please don't give up on me.  
I shall, since you're ignoring my advice anyway
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Move the call to display() to be outside of the loop, ie. move line 38 of the above, to be between line 40 and 41
0
 
jerry-2112Author Commented:
CEHJ,
 I have to look half your words up to understand what your saying ! You're talking above my head ! I try !
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now