0

Firstly, Im new to ArrayList. I have this program where the SalesPerson object will travel to 20 cities throughout the world. For every visit, the program will ask the user to enter name of the city, size of city, and postal code. This is done using for loop and ArrayList. But when i want to display all the cities, it print out weird character. May I know where did I went wrong?

Below is the sample output.

Enter name of city: Singapore

Enter size of city: Small

Enter postal code: 132115

==

Enter name of city: Indonesia

Enter size of city: Big

Enter postal code: 111222

==

Cities Visited: [travellingapp.City@42a57993, travellingapp.City@75b84c92]

This is my code:

TravellingApp.java

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        SalesPerson s1 = new SalesPerson();

        for(int j=1; j<=20; j++)
        {
            System.out.print("Enter name of city: ");
            String name = sc.nextLine();

            System.out.print("Enter size of city: ");
            String size = sc.nextLine();

            System.out.print("Enter postal code: ");
            int postalCode = sc.nextInt();
            sc.nextLine();

            System.out.println("___________________________________________");



            City c1 = new City(name, size, postalCode);
            s1.addCity(c1);

        }
        System.out.print("Cities Visited: "+ s1.returnListOfCities());

    }

SalesPerson.java (Class)

public class SalesPerson {

    private ArrayList<City>listOfCities = new ArrayList<City>();

    public void addCity(City c1)
    {
        listOfCities.add(c1);
    }

    public ArrayList<City> returnListOfCities()
    {
        return listOfCities;
    }
}

City.java (Class)

public class City {

    private String nameOfCity;
    private String sizeOfCity;
    private int postalCode;

    public City(String nameOfCity, String sizeOfCity, int postalCode)
    {
        this.nameOfCity = nameOfCity;
        this.sizeOfCity = sizeOfCity;
        this.postalCode = postalCode;
    }

    public String getNameOfCity() {
        return nameOfCity;
    }

    public String getSizeOfCity() {
        return sizeOfCity;
    }

    public int getPostalCode() {
        return postalCode;
    }

    public void setNameOfCity(String nameOfCity) {
        this.nameOfCity = nameOfCity;
    }

    public void setSizeOfCity(String sizeOfCity) {
        this.sizeOfCity = sizeOfCity;
    }

    public void setPostalCode(int postalCode) {
        this.postalCode = postalCode;
    }   


}
  • 1
    You should implement a `toString()` method in class `City` which returns a string representation of a city-object. for example: `String toString() { return nameOfCity;}` Then when you'll print an array/list of cities - you'll have a nice printing like you want. – Nir Alfasi Jan 14 '17 at 03:43
  • @alfasin Great, it works. Thank you for the explanation. One more thing, can I ask for advice or tips on how to find duplicates and display unique cities, the effective way? – Hakim Bajuri Jan 14 '17 at 03:59
  • for that you'll want to implement `equals()` and `hashcode()` for more details read the following or simply google it:http://crunchify.com/how-to-override-equals-and-hashcode-method-in-java/ – Nir Alfasi Jan 14 '17 at 04:08

2 Answers2

2

When you call s1.returnListOfCities(), the method returns an arraylist of City objects. This format is not recognized by System.out.println().

You could use an Iterator to iterate the Arraylist.

    Iterator<City> iterator = s1.returnListOfCities().iterator();
    while(iterator.hasNext()){
      System.out.println(iterator.next().getNameOfCity());
    }
Malith
  • 685
  • 1
  • 8
  • 14
  • The `ArrayList` inherits the `toString()` from `AbstractCollection`, which returns `[ n , n1 , n2 , ... , nn ]`, and as of Java 7 (at some point) the `System.out::println` method uses the closest `toString` implementation of any class. – J. Pichardo Jan 14 '17 at 15:08
1

Every class in Java extend from Object which has a toString() method, this returns a string like the ones you saw, and the System.out.println() method uses it, so if you haven't implemented it in your class it will use the one from Object (Polymorphism).

Something like

public String toString(){
   return "City { "+nameOfCity+", " + sizeOfCity +", "+postal code+"}";
}
J. Pichardo
  • 3,077
  • 21
  • 37