Array problems (printing strings)
Hey, I'm very new to programming and I just recently starting programming using Eclipse and I've having some trouble getting my array to print String as opposed to addresses. I haven't messed around too much with arrays so I think some of you may be able to point out where I went wrong fairly quickly.
I'm making a small text based RPGWhen the player chooses to start the game I have it print out the description of the part of town they're in and the exits they can choose from, however when it prints the result is:
Code Java:
Exits@1dd7056
Exits@fa3ac1
Exits@276af2
Exits@1de3f2d
Here is my code so far that is related to the Array of Exits:
---------------------------------------------------------
Code Java:
import java.util.ArrayList;
public ArrayList<Exits> Exits = new ArrayList<Exits>();
public Exits(String dir, Room room) <--- dir is the String that tells the player where that exit leads to
{
Direction = dir;
Room = room; <-- Room is what room the player is in.
}
Exits ToMageQuarters = new Exits( "Exit", MageQuarters );
Exits ToMarketPlace = new Exits( "Exit to Market Place", MarketPlace );
Exits ToMilitaryAcademy = new Exits( "Exit to Military Academy", MilitaryAcademy );
Exits ToMainGate = new Exits( "Exit to Main Gate", MainGate );
Exits ToAbandonedGhetto = new Exits( "Exit to Abandoned Ghetto", AbandonedGhetto );
Exits ToTownCenter = new Exits( "Exit to TownCenter", TownCenter );
Exits ToSWoutskirts = new Exits( "Exit to South West Outskirts", SWoutskirts );
Exits ToSEoutskirts = new Exits( "Exit to South Eest Outskirts", SEoutskirts );
Exits ToNEoutskirts = new Exits( "Exit to North East Outskirts", NEoutskirts );
TownCenter.Exits.add(ToMageQuarters);
TownCenter.Exits.add(ToMarketPlace);
TownCenter.Exits.add(ToMilitaryAcademy);
TownCenter.Exits.add(ToMainGate);
---------------------------------------------------------
Now when I go and try to get it printed out to the console I do:
Code Java:
for( int i = 0; i < currentRoom.Exits.size(); )
{
System.out.println( currentRoom.Exits.get(i) );
i++;
}
---------------------------------------------------------
Any help would be highly appreciated. Sorry of the mistake is obvious I'm still a very new programmer.
Re: Array problems (printing strings)
You didn't define the toString() method of Exits. Java is using Object's default toString() method, which prints out that stuff.
Re: Array problems (printing strings)
Quote:
Originally Posted by
helloworld922
You didn't define the toString() method of Exits. Java is using Object's default toString() method, which prints out that stuff.
What do you mean by that? Care to give an example?
Re: Array problems (printing strings)
Code Java:
public class Exits
{
// ... I left out the rest of the Exits class, but you'd obviously want to keep that
public String toString()
{
return "this is an exit object"; // you're actual toString method should return specific information you want in a format you want
}
}