Java ArrayList 名称 "Cannot Find Symbol" 和 "No Suitable Method" 用于 PrintWriter

Java ArrayList names "Cannot Find Symbol" and "No Suitable Method" for PrintWriter

环顾四周,我没能在别处找到这两个问题的答案。如果他们已经找到了,但我找不到他们,请指点我正确的方向。

我在我的程序中使用了三个ArrayList 来存储客户的订单信息。当我尝试调用它们时,每个 ArrayList 的变量名称出现 "cannot find symbol" 错误。

在该方法的最后,我尝试将帐单金额写入文件,但出现 returns "no suitable method found" 错误。

错误位置在注释中注明,错误消息的副本在末尾。

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;
     }
   }
}

错误信息是:

CustomerAccount.java:136: error: cannot find symbol
      String name = items(0);
                    ^
  symbol:   method items(int)
  location: class CustomerAccount

CustomerAccount.java:216: error: no suitable method found for write(double)
            order.write(baseTotal);
                 ^
    method Writer.write(int) is not applicable
      (argument mismatch; possible lossy conversion from double to int)
    method Writer.write(char[]) is not applicable
      (argument mismatch; double cannot be converted to char[])
    method Writer.write(String) is not applicable
      (argument mismatch; double cannot be converted to String)
    method PrintWriter.write(int) is not applicable
      (argument mismatch; possible lossy conversion from double to int)
    method PrintWriter.write(char[]) is not applicable
      (argument mismatch; double cannot be converted to char[])
    method PrintWriter.write(String) is not applicable
      (argument mismatch; double cannot be converted to String)

您应该使用 ArrayList get 方法来访问数组的元素。`

String name = items.get(0);          
String service = items.get(1);

看起来您没有向这段代码中的 ArrayList 添加任何内容。

至于第二个错误,你不可以写double。尝试将 double 转换为 String 然后写入它。

order.write(String.valueOf(baseTotal));

`

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

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++) {
 ......................

productPriceproductPrice 是数组。所以使用 length 检查数组的长度。

PrinerWriter#write(String) 不是 double

    order.write(""+baseTotal);
    order.write(""+taxTotal);
    order.write(""+grandTotal);
    //All three .write() statements return error