Domanda

I've done this (almost) exact same returning of an array in this same project, with one class (PlayerArray) it compiles but for some reason when I try returning an array in my (Planet) class it gives me the error "Incompatible Types" but I cannot understant why that is, I mean the method returns an array and it does return an array? If someone could explain the differences and why one works and the other complains about "Incompatible Types" then that would be great. Thanks! Here is some of my code.

Here's my launcher.

public class Launcher
{
private static Planet myTest;
private static PlanetInfo myPlanetInfo;
private static PlanetInfo[] planetArray;
private static Player[] arr; //NEED THIS FOR arr = myArray.getPlayerArray(); to work..
public static void main(String[] args)
{
    Planet myPlanetArray = new Planet();
    PlayerArray myArray = new PlayerArray();
    myPlanetArray.getPlanetArray();
    myArray.getPlayerArray();
    planetArray = myPlanetArray.returnCost();/////////////////////THIS IS THE BROKEN LINE
    arr = myArray.getPlayerArray(); //holy moses this worked...
    System.out.println("player 1's ID: " + arr[0].viewID());
    System.out.println("player 1's balance: " + arr[0].viewBalance());
    arr[0].playerGo();
}
}

Player array bellow works..

public class PlayerArray
{
Scanner scan = new Scanner(System.in);
private int numbHuman;
private Player[] arr;
private String[] userName;
private int[] userID;
private int startingMoney;
private int startingPosition;
private int b;
public PlayerArray()
{
    Scanner scan = new Scanner(System.in);
    System.out.println("How many players wish to play? Values of 2 through 8 accepted.");
    numbHuman = scan.nextInt();
    System.out.println(numbHuman + " players selected.");
    while (numbHuman < 2 || numbHuman > 8)
    {
        System.out.println("Invalid entry, try again.");
        numbHuman = scan.nextInt();
    }       
    arr = new Player[numbHuman];
    userName = new String[numbHuman];
    userID = new int[numbHuman];
    startingMoney = 1500;
    startingPosition = 0;
    b=0;
    for(int i = 0; i < arr.length; i++)
    {

        userID[i] = b;
        System.out.println("Player " + (i + 1) + ", Please enter your first name:");
        userName[i] = scan.next();
        arr[i] = new Player(userName[i],startingMoney,userID[i],startingPosition);
        b++;
    }
}

public int viewNumbHuman()
{
    return numbHuman;
}

public Player[] getPlayerArray()
{   
    return arr; //WORKS HERE...
}
}

and here's the class that won't compile...

public class Planet
{
Scanner scan = new Scanner(System.in);

private static Player[] arr;

private static Planet[] planetArray;

private int cost;

private int playerPosition;

public Planet()
{

    Planet testPlanet = new Planet();
    PlanetInfo[] planetArray;
    planetArray = new PlanetInfo[40];

    planetArray[1] = new PlanetInfo("Tatooine Mos Eisley",60,1,1,-1,2,0,0);
    planetArray[3] = new PlanetInfo("Tatooine Mos Espa",60,3,1,0,-1,0,0);

    //MORE of this, not important


    planetArray[37] = new PlanetInfo("Coruscant Jedi Temple",350,37,8,-1,35,0,0);
    planetArray[39] = new PlanetInfo("Coruscant Senate",400,39,8,-1,50,0,0);
}

public PlanetInfo[] getPlanetArray()
{
    return planetArray;
}

public int testCost()
{
    return cost;
}

public int returnCost()
{
    return planetArray[arr[0].viewPosition()].testCost(); //**** HERE *WAS* THE ISSUE
}
/*
public int what()
{
return planetArray[arr[0].viewPosition()];
}
*/
È stato utile?

Soluzione

The field planetArray is declared with

private static Planet[] planetArray;

and you're trying to return it in a method with signature

public PlanetInfo[] getPlanetArray()

Your method

public void Planets()

has a local variable of the type you're expecting, but that has no relation to the getPlanetArray method.

The Planets method is also a confusing method, as it looks like you might have intended to make a constructor, but gave it the wrong signature to be a constructor and instantiated a Planets object within it.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top