Dong Jun Dong Jun - 10 months ago 45
Java Question

Passing arrays between methods and calling them in main

I want to pass an array from one method to another and call that method in the array. However, it shows where

is not applicable for arguements. How would I fix this code?

I'm trying to pass
double[] price

import java.util.*;
public class LabQuiz3 {
public static void main(String[] args) {
static void initialize() {
String[] name=new String[10];
System.out.println("Name of the grocery? ");
Scanner scan=new Scanner(;
for (int i=0;
i < 10;
i++) {
double[] price=new double[10];
System.out.println("Price of each grocery? ");
Scanner sc=new Scanner(;
for (int j=0;
j < 10;
j++) {
double b=sc.nextDouble();
static void increaseCost(double[] price) {
for (int c=0;
c <10;
c++) {
double k=price[c] * .07;

Answer Source

In your main method you have to get the prices from the initialize() method:

public static void main(String[] args) {
    double[] prices = initialize();

And in the initialize method you have to return the prices array:

static double[] initialize() {
   //all your code
   return price;

A few other things to remark:

  • maybe rename your price variable to prices as it is a list of prices
  • you are mixing a lot of concerns (reading, printing, keeping state, etc.), normally you should keep those things separated
  • reading in the names of the groceries and the prices should probably be a seperate concern and reside in an own method, you lose track of the names since you are not returning them in any way
  • the for loop is strangely formatted, normally the full loop goes on one line

There are other things, but it's probably just a lab quiz.