JAVA: ONE error with amountList.length;?




Asked on October 16, 2013
I have ONE error I cannot figure out. 
The line of code is "for (int i=0; i < amountList.length; i++)" and the error says "Length cannot be resolved, or is not a field"
The entire code is below. PLEASE let me know WHY I have this error and what should be written.

package index;

import java.util.*;
import java.text.DecimalFormat;
import java.util.ArrayList;

public class threeArrayLists 
{
//declared arrays 
private ArrayList<Double> priceList = null;
private ArrayList<Double> quantityList = null;
private ArrayList<Double> amountList = null;

//here are the final arrays with content
final double[] Price_ARRAY = { 10, 14, 13, 16, 18, 9, 6, 18, 12, 3};
final double[] Quantity_ARRAY = {4.1, 8.15, 6.2, 7.35, 9.3, 15.3, 3.5, 5.4, 

2.9, 4.8};

public threeArrayLists()
{
//this calls the extend method
extend(Price_ARRAY, Quantity_ARRAY);
}

public void extend(double[] priceArray, double[] quantityArray)
{
//objects initialized
priceList = new ArrayList<Double>();
quantityList = new ArrayList<Double>();
amountList = new ArrayList<Double>();

// product of the quantity by price, into the subsequent elements in amount array

for (int i = 0; i < priceArray.length; i++) //here is error LENGTH
{
priceList.add(priceArray[i]);
quantityList.add(quantityArray[i]);
amountList.add(priceArray[i]*quantity…
}//end of statement

display();

}


Replied on October 17, 2013
first error:  amountList.add(priceArray[i]*quantity

quantity not defined.

second error: display method  is not defined.  I corrected the error. you can check it

package index;

import java.util.ArrayList;

public class threeArrayLists 
{
//declared arrays 
private ArrayList<Double> priceList = null;
private ArrayList<Double> quantityList = null;
private ArrayList<Double> amountList = null;

//here are the final arrays with content
final double[] Price_ARRAY = { 10, 14, 13, 16, 18, 9, 6, 18, 12, 3};
final double[] Quantity_ARRAY = {4.1, 8.15, 6.2, 7.35, 9.3, 15.3, 3.5, 5.4, 

2.9, 4.8};

public threeArrayLists()
{
//this calls the extend method
extend(Price_ARRAY, Quantity_ARRAY);
}

public void extend(double[] priceArray, double[] quantityArray)
{
//objects initialized
priceList = new ArrayList<Double>();
quantityList = new ArrayList<Double>();
amountList = new ArrayList<Double>();

// product of the quantity by price, into the subsequent elements in amount array

for (int i = 0; i < priceArray.length; i++) //here is error LENGTH
{
priceList.add(priceArray[i]);
quantityList.add(quantityArray[i]);
amountList.add(priceArray[i]*quantityArray[i]);
}//end of statement

display();

}

void display(){
for (int i = 0; i < priceList.size(); i++) //here is error LENGTH
{
System.out.print("price: "+priceList.get(i));
System.out.print("quantity: "+quantityList.get(i));
System.out.print("amountL: "+amountList.get(i));
System.out.println();
}//end of statement

}
public static void main(String[] args) {
threeArrayLists ob = new threeArrayLists();
ob.display();
}
}


Output

price: 10.0quantity: 4.1amountL: 41.0
price: 14.0quantity: 8.15amountL: 114.10000000000001
price: 13.0quantity: 6.2amountL: 80.60000000000001
price: 16.0quantity: 7.35amountL: 117.6
price: 18.0quantity: 9.3amountL: 167.4
price: 9.0quantity: 15.3amountL: 137.70000000000002
price: 6.0quantity: 3.5amountL: 21.0
price: 18.0quantity: 5.4amountL: 97.2
price: 12.0quantity: 2.9amountL: 34.8
price: 3.0quantity: 4.8amountL: 14.399999999999999
price: 10.0quantity: 4.1amountL: 41.0
price: 14.0quantity: 8.15amountL: 114.10000000000001
price: 13.0quantity: 6.2amountL: 80.60000000000001
price: 16.0quantity: 7.35amountL: 117.6
price: 18.0quantity: 9.3amountL: 167.4
price: 9.0quantity: 15.3amountL: 137.70000000000002
price: 6.0quantity: 3.5amountL: 21.0
price: 18.0quantity: 5.4amountL: 97.2
price: 12.0quantity: 2.9amountL: 34.8
price: 3.0quantity: 4.8amountL: 14.399999999999999




Write Answer











©2024 concretepage.com | Privacy Policy | Contact Us