'Void' type not allowed here error

时间:2015-10-30 21:41:56

标签: java compiler-errors

import java.util.Scanner;
import java.util.ArrayList;
public class OnlineStore {
    String[] books = {
        "Intro to Java", "Intro to C++", "Intro to C++", "Perl"
    };
    String[] dvds = {
        "Snow White", "Cinderella", "Dumbo", "Bambi"
    };
    double[] booksPrices = {
        45.99, 89.34, 100.00, 25.0
    };
    double[] dvdsPrices = {
        19.99, 24.99, 17.99, 21.99
    };
    ArrayList < String > cartItems = new ArrayList < > ();
    ArrayList < Double > prices = new ArrayList < > ();
    java.util.Scanner input = new Scanner(System. in );
    int userChoice;
    public void displayMenu() {
        System.out.printf("**Welcome to the Mustangs Books and DVDs Store**/n/n" + "Choose from the following options:/n1 - Browse books inventory" + "and add a book to the cart/n2 - Browse DVDs inventory and add" + "a DVD to the cart/n3 - View Cart/n4 - Checkout/n9 - Canel order" + "and exit store");
    }
    public static void displayArrays(String[] itemsArray, double[] pricesArray, String itemtype) {
        System.out.printf("Inventory Number    itemType     Prices" + "------------------------------------------" + " 1" + itemsArray[0] + pricesArray[0] + " 2" + itemsArray[1] + pricesArray[1] + " 3" + itemsArray[2] + pricesArray[2] + " 4" + itemsArray[3] + pricesArray[3]);
    }
    public void getTotal(ArrayList < Double > prices) {
        double total = 0;
        double taxTotal;
        double tax;
        int i;
        for (i = 0; i < prices.size(); i++) {
            total += prices.get(i);
        }
        tax = total * .825;
        taxTotal = tax + total;
        System.out.print("Your total is " + taxTotal);
    }
    public int getInventoryNumber() {
        System.out.print("Enter the inventory number you wish to purchase or enter -1 if you wish to see the menu: ");
        userChoice = input.nextInt();
        if (userChoice == -1) {
            displayMenu();
        }
        int correctNumber = userChoice - 1;
        return correctNumber;
    }
    public String displayArrays(ArrayList < String > items, ArrayList < Double > prices) {
        int i;
        System.out.print("Items         Prices" + "----------------------");
        for (i = 0; i < items.size(); i++)
        System.out.printf("%d%s%f", i, items.get(i), prices.get(i));
        System.out.print("---------------------- \n" + "Total + tax" + getTotal(prices));
    }
    public void addToCart(int inventoryNumber, ArrayList < String > cart, String[] inventory) {
        cart.add(inventory[inventoryNumber]);
    }
    public void cancelOrder(ArrayList < String > cartItems, ArrayList < Double > prices) {
        cartItems.clear();
        prices.clear();
    }
    public void main(String[] args) {
        int userInput;
        do {
            displayMenu();
            userInput = input.nextInt();
            if (userInput != 1 && userInput != 2 && userInput != 3 && userInput != 4 && userInput != 9) {
                System.out.println("This option is not acceptable.");
            } else if (userInput == 1) {
                displayArrays(books, booksPrices, "books");
                int correctNumber = getInventoryNumber();
                addToCart(correctNumber, cartItems, books);
            } else if (userInput == 2) {
                displayArrays(dvds, dvdsPrices, "dvds");
                getInventoryNumber();
                int correctNumber = getInventoryNumber();
                addToCart(correctNumber, cartItems, dvds);
            } else if (userInput == 3) {
                displayArrays(cartItems, prices);
            } else if (userInput == 4) {
                getTotal(prices);
            } else {
                cancelOrder(cartItems, prices);
            }
        }
        while (userInput != 9);
    }
}

On line 59, I get the error "'void' type not allowed". I'm not really sure how to fix this, and I'd appreciate some help. I understand how a void cannot have a return, but I'm a bit lost on why this is happening.

2 个答案:

答案 0 :(得分:3)

The line in question is this one:

System.out.print("---------------------- \n" +
                "Total + tax" + getTotal(prices));

The reason you're getting the error is that getTotal isn't returning anything.

public void getTotal(ArrayList <Double> prices) {

If you want getTotal to return something, you might change the signature to..

public Double getTotal(ArrayList <Double> prices) {

and then adjust the end of the function appropriately. (add a return statement)

答案 1 :(得分:2)

This is the problem. getTotal method is of type void and you are trying to print it. Either change the return type or remove the print statement.

System.out.print("---------------------- \n" +
                "Total + tax" + getTotal(prices));