Java ArrayList名称“;找不到符号“;及;“没有合适的方法”;印刷作家
环顾四周后,我没有找到这些问题的答案。如果他们一直在,而我找不到他们,请告诉我正确的方向 我在程序中使用三个ArrayList来存储客户的订单信息。当我试图调用它们时,每个ArrayList的变量名都会出现“找不到符号”错误 在方法的末尾,我尝试将账单金额写入文件,它返回“找不到合适的方法”错误 错误位置在注释中注明,错误消息的副本在末尾Java ArrayList名称“;找不到符号“;及;“没有合适的方法”;印刷作家,java,arraylist,printwriter,cannot-find-symbol,Java,Arraylist,Printwriter,Cannot Find Symbol,环顾四周后,我没有找到这些问题的答案。如果他们一直在,而我找不到他们,请告诉我正确的方向 我在程序中使用三个ArrayList来存储客户的订单信息。当我试图调用它们时,每个ArrayList的变量名都会出现“找不到符号”错误 在方法的末尾,我尝试将账单金额写入文件,它返回“找不到合适的方法”错误 错误位置在注释中注明,错误消息的副本在末尾 import java.io.*; import java.util.*; import java.lang.*; import java.io.FileNo
import java.io.*;
import java.util.*;
import java.lang.*;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.ArrayList;
import java.lang.Integer;
import java.lang.Double;
import java.lang.String;
public class CustomerAccount extends Customer {
static ArrayList<String> items = new ArrayList<String>();
static ArrayList<Double> prices = new ArrayList<Double>();
static ArrayList<Integer> quantities = new ArrayList<Integer>();
public boolean DetailedBill(String fileName) {
String name = items(0); //ERROR APPEARS HERE
String service = items(1); //ERROR APPEARS HERE
String confirmation;
int zipcode = quantities(0); //ERROR APPEARS HERE
double servicePrice = prices(0); //ERROR APPEARS HERE
double baseTotal;
double taxTotal;
double grandTotal;
boolean confirm;
final double TAX = 0.06;
final int ARRAY_SIZE = 5;
String[] productName = new String[ARRAY_SIZE];
int[] productQuantity = new int[ARRAY_SIZE];
double[] productPrice = new double[ARRAY_SIZE];
double[] productTotal = new double[ARRAY_SIZE];
for(int i = 2, y = 0; i < items.size(); i++, y++) {
productName[y] = items(i); //ERROR APPEARS HERE
}
for(int i = 1, y = 0; i < prices.size(); i++, y++) {
productPrice[y] = prices(i); //ERROR APPEARS HERE
}
for(int i = 1, y = 0; i < quantities.size(); i++, y++) {
productQuantity[y] = quantities(i); //ERROR APPEARS HERE
}
for(int i = 0; i < productPrice.size(); i++) {
//ERROR APPEARS HERE but points at .size()
productTotal[i] = productPrice[i] * productQuantity[i];
}
baseTotal = servicePrice + productTotal[0] + productTotal[1] + productTotal[2] + productTotal[3] + productTotal[4] + productTotal[5];
taxTotal = baseTotal * TAX;
grandTotal = baseTotal + taxTotal;
System.out.printf("%s's Detailed Bill:\n", name);
System.out.printf("%s service fee comes to: $.2f.\n", service, servicePrice);
for(int i = 0; i < productTotal.size(); i++) {
//ERROR APPEARS HERE but points at .size()
System.out.printf("%d %s at $%.2f each comes to $%.2f.\n", productQuantity[i], productName[i], productPrice[i], productTotal[i]);
}
System.out.printf("Total before tax: $%.2f.\n", baseTotal);
System.out.printf("Tax: $%.2f.\n", taxTotal);
System.out.printf("Balance due: $%.2f.\n", grandTotal);
Scanner input = new Scanner(System.in);
System.out.printf("Confirm? (y/n) ");
confirmation = input.nextLine();
while(!(input.equals('Y')) && !(input.equals('y')) && !(input.equals('N')) && !(input.equals('n'))) {
System.out.printf("Confirm? (y/n) ");
confirmation = input.nextLine();
}
if((input.equals('Y')) || (input.equals('y'))) {
PrintWriter order = new PrintWriter(new BufferedWriter(new FileWriter(fileName, true)));
order.write(baseTotal);
order.write(taxTotal);
order.write(grandTotal);
//All three .write() statements return error
order.close();
confirm = true;
return confirm;
}
else {
//if order cancelled, return cancellation to calling method
confirm = false;
return confirm;
}
}
}
您应该使用ArrayList get方法来访问数组的元素
String name = items.get(0);
String service = items.get(1);
在这段代码中,您似乎没有向ArrayList添加任何内容
至于第二个错误,你不允许写双精度。尝试将double转换为字符串,然后写入它
order.write(String.valueOf(baseTotal));
`
ArrayList#get(int)
从列表对象获取值
productName[y] = items.get(i); //ERROR APPEARS HERE
productPrice[y] = prices.get(i); //ERROR APPEARS HERE
productQuantity[y] = quantities.get(i); //ERROR APPEARS HERE
for(int i = 0; i < productPrice.length; i++) {
//ERROR APPEARS HERE but points at .size()
productTotal[i] = productPrice[i] * productQuantity[i];
}
for(int i = 0; i < productTotal.length; i++) {
......................
您需要
项。获取(o)
而不是项(0)
和.length
而不是.size()
,以及顺序。写入(Double.toString(baseTotal))代码>我建议您参考,如果您对如何使用特定类调用特定函数感到好奇,那么这应该是您的第一个调用端口。它成功了。谢谢你的帮助。这只回答了一半问题。那些PrintWriter
调用呢?我确实用另一种方法添加了它们,但不想把所有的代码都放在那里。这只回答了一半的问题。打给PrintWriter
的电话是什么?@Makoto,谢谢。我更新了我的答案。
String name = items.get(0); //ERROR APPEARS HERE
String service = items.get(1); //ERROR APPEARS HERE
int zipcode = quantities.get(0); //ERROR APPEARS HERE
double servicePrice = prices.get(0); //ERROR APPEARS HERE
productName[y] = items.get(i); //ERROR APPEARS HERE
productPrice[y] = prices.get(i); //ERROR APPEARS HERE
productQuantity[y] = quantities.get(i); //ERROR APPEARS HERE
for(int i = 0; i < productPrice.length; i++) {
//ERROR APPEARS HERE but points at .size()
productTotal[i] = productPrice[i] * productQuantity[i];
}
for(int i = 0; i < productTotal.length; i++) {
......................
order.write(""+baseTotal);
order.write(""+taxTotal);
order.write(""+grandTotal);
//All three .write() statements return error