magna_nz
magna_nz

Reputation: 1273

Linking two classes together

I have two classes here that I'm trying to link together but can't get it to work. One is called smokers one is called Pair.

My compiler is spewing errors saying that it can't find symbol pair.

I have placed them both in the same package. Not sure why it isn't registering it.

package smokerz;

import java.util.*;


public class smokers{

    //initialise
    //create a node inner class which has the nodes position
    //make 2D array of nodes
    //
    public static void main(String[]args){
        initialise();

    }

    public static void initialise(){
        List<Pair<Integer, Integer>> pairedCoordinates = new ArrayList<Pair<Integer, Integer>>();
        Scanner scan = new Scanner(System.in);
        int line = 0;

        //for board boundaries
        int boardX;
        int boardY;
        while (scan.hasNextLine()){
            //means we're reading the board coordinates
            if (line == 0){
                boardX = scan.nextInt();
                boardY = scan.nextInt();
            }
            int nonSmokersPosX;
            int nonSmokersPosY;
            nonSmokersPosX = scan.nextInt();
            nonSmokersPosY = scan.nextInt();
            pairedCoordinates.add(new Pair(nonSmokersPosX, nonSmokersPosY));
            line++;
        }


package smokerz;

public class Pair<X, Y>{

    private X xcoord;
    private Y ycoord;

    public Pair(X xcoord, Y ycoord){
        this.xcoord = xcoord;
        this.ycoord = ycoord;

    }
    public X getXCoord(){
        return xcoord;
    }

    public Y getYCoord(){
        return ycoord;
    }
    public void setXCoord(X x){
        this.xcoord = x;
    }
    public void setYCoord(Y y){
        this.ycoord = y;
    }
}

Upvotes: 2

Views: 1020

Answers (3)

Ashley Frieze
Ashley Frieze

Reputation: 5443

The smokers class needs at least two }'s at the end of it to finish the code.

The package for pair doesn't need a ; after it.

Perhaps the split between files in your code isn't made clear in the question.

Upvotes: 0

paulsm4
paulsm4

Reputation: 121659

Strong suggestion: name your first class "Smokers" (capital "S").

Q: Did "Pair.java" actually compile? I suspect not.

Please try compiling "Pair", and please update your post with the error message (if applicable).

======================================================

ADDENDUM:

It just occurred to me what's going wrong - you're using packages (good!), but you're forgetting to take them into consideration when you compile from the command line (something an IDE would do for you automagically).

Please do this:

1) mkdir smokerz # Create a subdirectory for your Java package

2) cp smokers.java Pair.java smokerz # Copy your source to "smokerz"

3) javac smokerz\Pair.java # Compile your two files in the directory *above*

javac smokerz\smokers.java

4) Run your program

java smokerz.smokers

Look here for more details:

http://www.jarticles.com/package/package_eng.html

Upvotes: 1

mr_kolar
mr_kolar

Reputation: 1

It could possibly be because you have your initialise() as static. If it doesn't have to be static, take static out and try that.

Upvotes: 0

Related Questions