user3212138
user3212138

Reputation:

Passing an array as a parameter to a method

Here is my half done program:

import java.util.*;

public class Sclad {

public static void vavediMasiv(int col[], double cenaE[], double cenaD[]){
    Scanner sc = new Scanner(System.in);
    for (int i = 1; i < col.length; i++)
    {
        System.out.println("Molq vuvedete kolichestvoto ot produkt nomer " + i);
        col[i] = sc.nextInt();
        System.out.println("Kolichestvoto na produkta zadadeno kato: " + col[i]);
    }

    for (int i = 1; i < cenaE.length; i++)
    {
        System.out.println("Molq vuvedete cenata na edro na produkt nomer " + i);
        cenaE[i] = sc.nextDouble();
        System.out.println("Cenata na produkta na edro zadadeno kato: " + cenaE[i]);
    }

    for (int i = 1; i < cenaD.length; i++)
    {
        System.out.println("Molq vuvedete cenata na drebno na produkt " + i);
        cenaD[i] = sc.nextDouble();
        System.out.println("Cenata na produkta na drebno zadadeno kato: " + cenaD[i]);
    }

}
public static void calcPechalba(){
    for (int i = 1; i < col.length; i++)
        System.out.println("KUR");
}


public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    System.out.println("Vavedete broi na stokite prisustvashti v sklada...");
    int m = sc.nextInt();
    System.out.println("Vie izbrahte " + m + " za kolichestvo na stokite.");

    int col[] = new int[m+1];
    double cenaE[] = new double[m+1];
    double cenaD[] = new double[m+1];
    vavediMasiv(col[m+1], cenaE[m+1], cenaD[m+1]); // I GET AN ERROR HERE SAYING The method vavediMasiv(int[], double[], double[]) in the type Sclad is not applicable for the arguments (int, double, double)
}
}

I have commented the problematic part. I don't get why doesn't it accept as parameters. I have tried everything but it still won't accept the arrays. What can I do to fix this up or at least to compromise it and do it another way?

Upvotes: 0

Views: 83

Answers (6)

user2754122
user2754122

Reputation:

Your method vavediMasiv(int col[], double cenaE[], double cenaD[]) is expecting three arguments of type int array, double array and double array. While you are passing int, double and double as arguments. That's why it is giving error.

Upvotes: 0

tmarwen
tmarwen

Reputation: 16374

You are trying to pass an (int double double) parameters to a method which signature says it accepts arrays of (int double double).

Looking to your source code, you merely need to change the

  vavediMasiv(col[m+1], cenaE[m+1], cenaD[m+1]);

call to:

  vavediMasiv(col, cenaE, cenaD);

You should also note that the below method won't compile since there is no local variable col:

public static void calcPechalba(){
   for (int i = 1; i < col.length; i++)
    System.out.println("KUR");
   }

Sorry but I can't understand your methods logical flow to give a better shot.

BR.

Upvotes: 0

Paul Samsotha
Paul Samsotha

Reputation: 209132

You're passing values in the array and not the actual array. That's why you are getting the error int cant be converted to int[] because col[m +1] is an int value in the col array. Same with the other two arrays.

vavediMasiv(col[m+1], cenaE[m+1], cenaD[m+1])

all the above are values in the array. You want

vavediMasiv(col, cenaE, cenaD); 

You also might want to pass an array to this method also, to get rid of this error

public static void calcPechalba(int[] col){    <------
    for (int i = 1; i < col.length; i++)
        System.out.println("KUR");
}

Upvotes: 0

TheLostMind
TheLostMind

Reputation: 36304

See... You have defined

public static void vavediMasiv(int col[], double cenaE[], double cenaD[]) // expecting and accepting whole arrays as arguments.

and you are using :

 vavediMasiv(col[m+1], cenaE[m+1], cenaD[m+1]); // you are passing individual values of arrays instead of whole array. 

You have to use :

vavediMasiv(col, cenaE, cenaD); 

Upvotes: 1

Troubleshoot
Troubleshoot

Reputation: 1846

You're passing it int and double values. To pass it the int and double arrays you need to do this:

vavediMasiv(col, cenaE, cenaD);

For example, here you're passing it the m+1 element of the cenaD array, and not the array itself.

cenaD[m+1]

Upvotes: 3

StarsSky
StarsSky

Reputation: 6711

ou should pass the whole objects

vavediMasiv(col, cenaE, cenaD);

Upvotes: 0

Related Questions