Win a copy of Spring Boot in Practice this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Error while running program

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I have a program I am running for a project for my class. The project compiles just fine but when it gets to a certain point it blows up and gives me an error.

The error is

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.String; at Project02.calculate(Project02.java:106)
at Project02.main(Project02.java:79.


Here is my program:

import java.util.*;
import java.io.*;
import java.text.*;

// This class defines a program to process orders for a fast food restaurant.
// ---- Written by: Jon Huhtala
// --- Modified by: Andy Rayburn
public class Project02 {

// This method begins application processing.

public static void main(String[] args) {

// Variables.

double taxRate; // For holding the tax rate.
String reply; // For holding the user's latest keyboard entry.
ArrayList quantities; // For holding the item quantities for the
// customer order.

// These coordinated arrays hold item names and their prices

String[] items = {"Burgers", "Fries", "Drinks"};
double[] prices = {2.75, 1.50, 1.35};

// If the program did not receive a command line parameter, set the
// tax rate to .06 (meaning 6%). Otherwise, set the tax rate to the
// value of the parameter.

if (args.length == 0) {
taxRate = .06;
}
else {
String taxRateAsString = args[0]; // GET PARAMETER VALUE.
taxRate = Double.parseDouble (taxRateAsString); // CONVERT FROM String TO double.
}

// Loop to process one customer order.

do {

// Display item names and prices.

System.out.println("");
System.out.println("--------- ITEMS AND PRICES ---------");
System.out.println("");

// THIS LOOP SHOULD DISPLAY ITEM NAMES AND PRICES IN A FORMAT THAT
// MATCHES THE SAMPLE OUTPUT SHOWN BELOW.

for (int i = 0; i < items.length; i++) { // LIMIT BY SIZE OF items ARRAY.
System.out.println(" " + items[i] + "..." + Utility.moneyFormat(taxRate));
}

// Get order data.

System.out.println("");
System.out.println("------------ YOUR ORDER ------------");
System.out.println("");
quantities = new ArrayList();

// THIS LOOP SHOULD PROMPT FOR AND READ THE NUMBER OF EACH ITEM THE
// CUSTOMER WANTS TO BUY. IT SHOULD MATCH THE FORMAT OF THE SAMPLE
// OUTPUT SHOWN BELOW.

for (int i = 0; i < items.length; i++) { // LIMIT BY SIZE OF items ARRAY.
System.out.print(" Number of " + items[i] + ": ");
reply = Utility.readString();
quantities.add (items); // ADD TO THE quantities ArrayList.
}

// Calculate and display "Payment Due" information.

System.out.println("");
System.out.println("----------- PAYMENT DUE ------------");
System.out.println("");
double subtotal = calculate(prices, quantities);
System.out.println(" Subtotal: " + Utility.moneyFormat(subtotal));
double tax = subtotal * taxRate;
System.out.println(" Tax: " + Utility.moneyFormat(tax) +
" at " + Utility.percentFormat(taxRate)); // SHOW taxRate AS FORMATTED PERCENTAGE.
double totalDue = subtotal + tax;
System.out.println(" TOTAL: " + Utility.moneyFormat(totalDue));

// Ask the user if they want to process another order. If they reply with
// a String whose value is "YES", repeat this loop.

System.out.println("");
System.out.println("-------------- AGAIN? --------------");
System.out.println("");
System.out.print(" Enter YES to do another order: ");
reply = Utility.readString();
} while ( reply.toUpperCase() == "YES"); // ACCEPT "YES" IN ANY CASE (LIKE "yes") TO CONTINUE.
}

// This method can be called to calculate the non-taxable amount due for
// an order. It receives the array of prices and ArrayList of corresponding
// quantities as parameters.

public static double calculate(double prices[], ArrayList quantites) {
double total = 0;
for (int i = 0; i < prices.length; i++) { // LIMIT BY SIZE OF THE ARRAY.
double price = prices[i]; // GET THE ITEM'S PRICE.
int quantity = ((Integer)quantites.get(i)).intValue(); // GET CORRESPONDING QUANTITY ORDERED.
double extendedPrice = price * quantity;
total += extendedPrice;
}
return total;
}
}


Please help

Thanks
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at what you're adding to the ArrayList.
reply
    Bookmark Topic Watch Topic
  • New Topic