0

I'm trying to assign an item to mapTiles[x][i] each time through a loop like this: mapTiles[x][i] = mapCols[i]; But it's not working. This is the error I'm getting:

incompatible types - found java.lang.String but expected MapTile

My code:

public class Map {

MapTile[][] mapTiles;
String imageMap;
String rawMap;

// constructor 
public Map() {
    imageMap = "Map_DragonShrine.jpg";
    rawMap = "Dragon_Shrine.map";
    mapTiles = new MapTile[34][22];
}


// methods
public void loadMapFile() {

    rawMap = file2String(rawMap);

    // array used to hold columns in a row after spliting by space
    String[] mapCols = null;
    // split map using 'bitmap' as delimiter
    String[] mapLines = rawMap.split("bitmap");  
    // assign whatever is after 'bitmap'
    rawMap = mapLines[1];
    // split string to remove comment on the bottom of the file
    mapLines = rawMap.split("#");
    // assign final map
    rawMap = mapLines[0].trim();
    mapLines = rawMap.split("\\n+");

    for(int x = 0; x < mapLines.length; x++) {
        rawMap = mapLines[x] ;
        mapCols = rawMap.split("\\s+");
        for(int i = 0; i < mapCols.length; i++) {
            mapTiles[x][i] = mapCols[i];   
        }            
    }   
}     
}

This is the MapTile class that mapTiles is an object of. I'm not understanding how to pass new MapTile(mapTiles[i]) given what I have. I'm trying to wrap my head around the use of 2d array with class and instances of it.

MapTile class:

public class MapTile {
/**Solid rock that is impassable**/
boolean SOLID, 

/** Difficult terrain. Slow to travel over*/
DIFFICULT, 

/** Statue. */
STATUE,

/** Sacred Circle.  Meelee and ranged attacks are +2 and
    damage is considered magic.  */
SACRED_CIRCLE, 

/** Summoning Circle. */
SUMMONING_CIRCLE,

/** Spike Stones. */
SPIKE_STONES,

/** Blood Rock.Melee attacks score critical hits on a natural 19 or 20. */
BLOOD_ROCK,

/** Zone of Death.Must make a morale check if hit by a melee attack. */
ZONE_OF_DEATH,

/** Haunted.-2 to all saves. */
HAUNTED,

/** Risky terrain. */
RISKY,

/** A pit or chasm. */
PIT,

/** Steep slope. */
STEEP_SLOPE,

/** Lava. */
LAVA,

/** Smoke. Blocks LOS. */
SMOKE,

/** Forest. Blocks LOS. */
FOREST,

/** Teleporter. */
TELEPORTER,

/** Waterfall. */
WATERFALL,

/** Start area A. */
START_A,

/** Start Area B. */
START_B,

/** Exit A. */
EXIT_A,

/** Exit B. */
EXIT_B,

/** Victory Area A. */
VICTORY_A,

/** Victory Area B. */
VICTORY_B,

/** Temporary wall terrain created using an Elemental Wall or other means. */
ELEMENTAL_WALL;       

public MapTile() {
}

}

1
  • The error message says it clearly. Incompatible types. Maybe you want String[][] mapTiles Commented Nov 6, 2013 at 5:43

1 Answer 1

2

That is because mapTiles is a 2-d array of type MapTile whereas mapCols is a 1-d array of type String. You cannot assign a String value to a variable of MapTile type. They are incompatible types as the error message states.

You might probably want to do something like this

mapTiles[x][i] = new MapTile(mapCols[i]);
// where the MapTile constructor takes a String parameter.
Sign up to request clarification or add additional context in comments.

1 Comment

I'm having a hard time creating an instance of MapTile. 2d array with class and objects are taking awhile to wrap my head around. I posted the MapTile class code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.