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 3 of 3

Thread: Help with toString method and an addObject method?

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

    Default Help with toString method and an addObject method?

    I have a Room class and an Animal class. A Room can hold 10 Animals. Both rooms and animals have names. I'm having trouble with the Room class's toString method. I want it to return its [the room's] name and the names of all the animals in the room. My addAnimal method in the Room isn't coded well, I can't figure out the correct way to go about it. Originally I was trying to cycle through the array 'animals' with a for loop to put an animal in each slot of the array but my method was giving me errors. The code for that was something like:

    public void addAnimal(Animal a){
    for(int x=0; x < animals.length; x++){
    animals[x] = a;
    }
    }

    That was giving me the incorrect input I wanted, so I tried manually inserting a different Animal object into each slot of the array, 'animals'.

    My main class has comments on what should be outputting...

    public class Main {
    public static void main(String[] args) {
    Room myroom = new Room("Bathroom");
    myroom.addAnimal(new Animal("Dog"));
    myroom.addAnimal(new Animal("Cat"));
    myroom.addAnimal(new Animal("Bird"));
    String temp = myroom.toString();
    //should print nothing on the screen by this point
    System.out.println(temp);
    //should print on the screen the following:
    //Room Snygg 107 populated by:
    //Animal Dog
    //Animal Cat
    //Animal Bird
    }
    }

    Here are my Animal and Room Classes:


    public class Animal {
    private Room room;
    private String name;
    String toString;

    public Animal(String animalName){
    name = animalName;
    }

    public String getName(){
    return name;
    }
    public String toString(){
    return name;
    }
    }


    ------------------

    public class Room {
    private String name;
    private Animal [] animals;


    public Room(String roomName)
    {
    name = roomName;
    animals = new Animal[10];
    }


    public void addAnimal(Animal a){

    Animal b = new Animal("");
    Animal c = new Animal("");
    Animal d = new Animal("");
    Animal e = new Animal("");
    Animal f = new Animal("");
    Animal g = new Animal("");
    Animal h = new Animal("");
    Animal i = new Animal("");
    Animal j = new Animal("");
    animals[0] = a;
    animals[1] = b;
    animals[2] = c;
    animals[3] = d;
    animals[4] = e;
    animals[5] = f;
    animals[6] = g;
    animals[7] = h;
    animals[8] = i;
    animals[9] = j;



    }
    public String toString(){
    String names = "";
    for(Animal animal : animals){
    names = animal.toString;
    }
    return "Room: " + name + ", populated by... \n" + names;
    }
    }



    My current output with this code is:

    "Room: Snygg 107, populated by...
    null"


  2. #2
    Member
    Join Date
    Feb 2010
    Posts
    30
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Help with toString method and an addObject method?

    Please put your code in [code] tags.

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with toString method and an addObject method?

    Try reviewing your code to see what it's doing. For example, this method populates the entire array with the same Animal class. You'll need to write a few more lines of code to get the first index in the array that does not have an Animal and put the input Animal into that one slot.

    public void addAnimal(Animal a){ // Your method above
         for(int x=0; x < animals.length; x++){
              animals[x] = a;
         }
    }

    For your toString method, try something like this:

    public String toString() {
         String output = "";
         int i = 0;
         while ( i < animals.length ... ) { // loop through the animals array and check if this index has an animal
               // Add the name to the output value
               // don't forget your line breaks
         }
     
        System.out.println("Room " + this.name + " contains: " + output);
    }

Similar Threads

  1. Valid toString method?
    By dcshoecousa in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 4th, 2010, 11:55 AM
  2. Can i call init() method in destroy method.?
    By muralidhar in forum Java Servlet
    Replies: 1
    Last Post: October 22nd, 2010, 11:18 AM
  3. Looking for a method,
    By Time in forum Algorithms & Recursion
    Replies: 7
    Last Post: May 15th, 2010, 12:24 PM
  4. [SOLVED] toString() method
    By chronoz13 in forum Object Oriented Programming
    Replies: 12
    Last Post: January 19th, 2010, 06:44 AM
  5. Pretty new to Java can someone help with an addObject() problem?
    By alan120184 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 1st, 2009, 12:48 PM