user1845203
user1845203

Reputation: 21

Why do I get an error when stating my package?

I'm learning Java, and how to use multiple classes. I'm trying to get Enter your name. printed on the console, the user says their name, then I print the message ("Hello, ) + name).

When I try to run it, I get an error. (Exception in thread "main" java.lang.Error: Unresolved compilation problem: at bucky.ParaMain.main(ParaMain.java:7)).

Below is my code from class 1, named ParaMain.java.

import java.util.Scanner;

package bucky;

public class ParaMain {

public static void main(String[] args) {


    Scanner input = new Scanner(System.in);
    ParaS secObject = new ParaS();

    System.out.println("Enter your name.");
    String name = input.nextLine();

    secObject.SimpleMessage(name);

}

}

Here is my code from the second class, named ParaS.java.

package bucky;

public class ParaS {
public void SimpleMessage(String name) {
    System.out.println("Hello, " + name);
}
}

Please help me to solve this issue. Thanks!

Upvotes: 0

Views: 102

Answers (5)

Muneeb Tariq
Muneeb Tariq

Reputation: 35

Including the Designated Package will resolve the problem IF you are using Eclipse or Net Beans it will give error suggestion to you press Ctrl+1 to resolve the problem

Upvotes: 0

Omoro
Omoro

Reputation: 972

In a java source file, there should be only one package statement and it should be the first statement in your file. In class ParaMain reverse these statements:

import java.util.Scanner;

package bucky;

should be the other way round.

package bucky;

import java.util.Scanner;

Upvotes: 0

Stephen Buttolph
Stephen Buttolph

Reputation: 643

You simply need to change where it is. Move the package to before the imports. See below:

package bucky;

import java.util.Scanner;

public class ParaMain {

    public static void main(String[] args) {


        Scanner input = new Scanner(System.in);
        ParaS secObject = new ParaS();

        System.out.println("Enter your name.");
        String name = input.nextLine();

        secObject.SimpleMessage(name);

    }

}

Hope this helps. :)

Upvotes: 0

jaqen0
jaqen0

Reputation: 52

Package declaration goes before imports

Upvotes: 1

grishnagkh
grishnagkh

Reputation: 49

As stated in the comment from Darshan Lila : the package declaration MUST be on first line, if present, then imports, then your class ;)

Upvotes: 1

Related Questions