Andrew Ng
Andrew Ng

Reputation: 90

Calling a class in main

Hi I seem to have a problem calling a class in a main. Can somebody point it out?

KilometerTabel.java

package pratikum31d;
public static double mijlToKilometer() {
    double mijl;
    mijl = 0;
    for (int i = 1; i < 11; i++) {
        mijl = i;
    }
    double kilometer = 1.609 * mijl;
    System.out.println(kilometer + " kilometer" + " dat is " + mijl + " mijl");
    return kilometer;
}

Main.java

package pratikum31d;
public class Main {

    public static void main(String[] args) {

        kilometer = mijlToKilometer();

    }
}

Upvotes: 0

Views: 5637

Answers (3)

Louis Wasserman
Louis Wasserman

Reputation: 198033

You never defined a variable called mijl in main. What value do you expect to get passed to mijlToKilometer?

===UPDATE ===

Your new code will have the following problems:

mijlToKilometer is still declared to expect an argument, so you won't be able to call it with no arguments. You must remove the double mijl from the definition of mijlToKilometer.

Your for loop doesn't do what you think it does, though I'm having a hard time identifying what it's supposed to do.

Upvotes: 2

nist
nist

Reputation: 1721

You must declare mijlToKilometer as public.

public static double mijlToKilometer(double mijl)

Upvotes: 1

Em Ae
Em Ae

Reputation: 8704

What are the packages for KilometerTabel and the main class? You haven't put any public/private/protected modifier before your static method. so by default, it will have default visibility. Which is visible within the package. make sure that you bave both classes in same package OR put a public keyword before methods.

secondly, can you please post exact exception?

Upvotes: 0

Related Questions