11 Replies Latest reply: Oct 4, 2006 5:59 PM by doremifasollatido RSS

    toString array and equals method

    807607
      I have an array and i need to get the toString to output the information. How do I get that to happen. This is what i ahve so far.
         public String toString(){ 
                //should indicate all cities 
                System.out.print("Cities :"); 
                for (int i = 0; i < cities.length -1; i++) 
                     return cities; 
      }


      Here is another piece of code I am finishing. Please tell me if the loop and equals method is correct but what is the value to return?
         public City getCity(String cityName){ 
                //returns the City with name cityName (must search for city in the array) 
                //if the city does not exist, it returns null 
                for(int i = 0; i < cities.length; i++) 
                     if(cityName.equals ("cityName")) 
                          return ; 
                       
                      else return null; 
                            } 
                  
      Message was edited by:
      ehj3000
        • 1. Re: toString array and equals method
          doremifasollatido
          Do you have a City class such that cities is an array of references to City objects?

          If so, getCities needs to use the following somewhere:
          cities.getName()

          What class is that toString() in?
          • 2. Re: toString array and equals method
            807607
            yes there is a city class

            the toString is in the world method
            • 3. Re: toString array and equals method
              doremifasollatido
              Okay, you need to concatenate the names of all of your cities (and whatever other details your city has that you want displayed by World's toString).

              Make a StringBuffer or StringBuilder. Append the city names to it, with commas (or whatever) separating them.
              StringBuffer cityNames = new StringBuffer("Cities : ");
              for (...)
              {
                 cityNames.append(cities.getName());
              cityNames.append(", ");
              }
              return cityNames.toString();

              Or, just append "cities[ i ]" if you want to include the whole output of City's toString method. You may want to make sure to skip the comma after the last city.
              • 4. Re: toString array and equals method
                807607
                thanks...can some help me with my getCity method?
                • 5. Re: toString array and equals method
                  doremifasollatido
                  thanks...can some help me with my getCity method?
                  You want to return the cities[ i ] for which cities[ i ].getName() equals the input cityName. You need to fix the "if" that you have to reflect that. Hint: change the input to ".equals". Then put something in the "return" statement.
                  • 6. Re: toString array and equals method
                    807607
                    i tried this but i get incompatiable types with the return
                           public City getCity (String cityName){
                          //returns the City with name cityName (must search for city in the array)
                          //if the city does not exist, it returns null
                             for(int i = 0; i < cities.length; i++) 
                                if(cities.getName().equals(cityName))
                    return cities[i].getName();
                    else
                    return null;
                    }
                    • 7. Re: toString array and equals method
                      doremifasollatido
                      Don't return the cities[ i ].getName()--just return the cities[ i ] itself.
                      • 8. Re: toString array and equals method
                        807607
                        ok this is the last method i need help completing...I am confused on calling the city class. And the line with name gives me a cannot find symbol.
                                public World(String name, City [] cities){
                              //creates a world with the cities passed in
                                 this.name = name;
                                 this.cities = new cities[8];
                              }
                        • 9. Re: toString array and equals method
                          doremifasollatido
                          ok this is the last method i need help
                          completing...I am confused on calling the city class.
                          And the line with name gives me a cannot find
                          symbol.
                                  public World(String name, City [] cities){
                          //creates a world with the cities passed in
                          this.name = name;
                          this.cities = new cities[8];
                          }
                          Did you define the World to have a name?
                          The allocation of the cities array should be:
                          this.cities = new City[8];
                          Then you'll need to copy the parameter "cities" to the "this.cities". You can use java.lang.System.arraycopy.
                          • 10. Re: toString array and equals method
                            807607
                            great my world class is fixed now im getting missing return statements on the two methods
                                   public City getCity (String cityName){
                                  //returns the City with name cityName (must search for city in the array)
                                  //if the city does not exist, it returns null
                                     for(int i = 0; i < cities.length; i++) 
                                        if(cities.getName().equals(cityName))
                            return cities[i];
                            else
                            return null;
                            }


                            public String toString(){
                            //should indicate all cities
                            System.out.println("Cities: ");
                            for (int i = 0; i < cities.length -1; i++)
                            return cities[i].getName();

                            }
                            • 11. Re: toString array and equals method
                              doremifasollatido
                              If your cities.length is 0, you won't return anything.

                              You should put { } around the body of your 'for' loops, even though they are only one line. Same for your 'if' and 'else' (you don't need 'else' here, but it's general advice).

                              getCity should have (null will only be returned if the city wasn't found [no cities exist, or specific city doesn't exist]).
                                      for(int i = 0; i < cities.length; i++)
                                      {
                                          if(cities.getName().equals(cityName))
                              {
                              return cities[i];
                              }
                              }
                              return null;

                              toString should be modified in a similar way.