Search code examples
javaarraylistprintwritercannot-find-symbol

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


After looking around, I haven't been able to find either of these questions answered elsewhere. If they have been and I couldn't find them, please point me in the right direction.

I use three ArrayLists in my program to store a customer's order information. When I try to call them, I get a "cannot find symbol" error for the variable names of each ArrayList.

At the end of the method, I try to write the bill amounts to the file and it returns "no suitable method found" error.

Error locations are noted a comments and a copy of the error messages is at the end.

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

The error messages are:

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)

Solution

  • You should be using the ArrayList get method to access the element of an array.`

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

    It also looks like that you don't add anything to the ArrayLists in this code.

    As for the second error, you are not allowed to write a double. Try converting the double to a String and then writing it.

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

    `