Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Array problems (printing strings)

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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:

    Exits@1dd7056
    Exits@fa3ac1
    Exits@276af2
    Exits@1de3f2d

    Here is my code so far that is related to the Array of Exits:

    ---------------------------------------------------------
    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:
    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.
    Last edited by James5955; January 11th, 2011 at 10:01 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Array problems (printing strings)

    Quote Originally Posted by helloworld922 View Post
    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?

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Array problems (printing strings)

    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
        }
    }

  5. The Following User Says Thank You to helloworld922 For This Useful Post:

    James5955 (January 13th, 2011)

Similar Threads

  1. Printing the Max and Min in an Array
    By bonbon242 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 29th, 2010, 08:28 AM
  2. creating an array of strings?
    By Jason in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 19th, 2010, 08:28 AM
  3. Problems with Input Strings
    By Bekuraryou1228 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 4th, 2010, 04:34 PM
  4. [SOLVED] Printing Array without printing empty elements
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2010, 02:41 AM
  5. Problems with File Reader (Strings and 2D Array Storage)
    By K0209 in forum File I/O & Other I/O Streams
    Replies: 44
    Last Post: January 12th, 2010, 09:48 AM