-3

环顾四周后,我无法在其他地方找到这些问题的答案。如果它们已经存在并且我找不到它们,请指出正确的方向。

我在我的程序中使用了三个 ArrayList 来存储客户的订单信息。当我尝试调用它们时,每个 ArrayList 的变量名称都会出现“找不到符号”错误。

在方法结束时,我尝试将账单金额写入文件并返回“找不到合适的方法”错误。

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

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)
4

2 回答 2

3

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

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

看起来您没有在此代码中向 ArrayLists 添加任何内容。

至于第二个错误,你不能写一个double。尝试将双精度转换为字符串,然后编写它。

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

`

于 2015-07-16T17:03:29.587 回答
0
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++) {
 ......................

productPrice并且productPrice是数组。所以length用来检查数组的长度。

PrinerWriter#write(String)不是double

    order.write(""+baseTotal);
    order.write(""+taxTotal);
    order.write(""+grandTotal);
    //All three .write() statements return error
于 2015-07-16T17:05:40.140 回答