java - 'Void' type not allowed here error -
this question has answer here:
import java.util.scanner; import java.util.arraylist; public class onlinestore { string[] books = { "intro java", "intro c++", "intro 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 mustangs books , dvds store**/n/n" + "choose following options:/n1 - browse books inventory" + "and add book cart/n2 - browse dvds inventory , add" + "a dvd 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; (i = 0; < prices.size(); i++) { total += prices.get(i); } tax = total * .825; taxtotal = tax + total; system.out.print("your total " + taxtotal); } public int getinventorynumber() { system.out.print("enter inventory number wish purchase or enter -1 if wish see 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" + "----------------------"); (i = 0; < 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; { displaymenu(); userinput = input.nextint(); if (userinput != 1 && userinput != 2 && userinput != 3 && userinput != 4 && userinput != 9) { system.out.println("this option 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, error "'void' type not allowed". i'm not sure how fix this, , i'd appreciate help. understand how void cannot have return, i'm bit lost on why happening.
the line in question one:
system.out.print("---------------------- \n" + "total + tax" + gettotal(prices));
the reason you're getting error gettotal isn't returning anything.
public void gettotal(arraylist <double> prices) {
if want gettotal return something, might change signature to..
public double gettotal(arraylist <double> prices) {
and adjust end of function appropriately. (add return statement)
Comments
Post a Comment