-1

I'm brand new to programming and Java, I'm doing a school project with very strict guidelines. I'm sure there's a more efficient way to do my code, but that's not my issue. When I try to print my array at the bottom of main, I get

"Country@10b28f30, Country@3ad6a0e0, Country@60dbf04d",.... and so on.

I know the array is loading, because when // System.out.println ("object is: " + name + " " + capital + " " + region + " " + region_Nbr + " " + capital_population);

runs, it prints all elements of the array as it's being built. I've keep reading something about having to override toString, I've tride multiple ways to print the array, none work. Thanks in advance.

public class Main {

/**
 * @param args the command line arguments
 */

private int size = 43;
private static Country[] countryInfo = new Country[43];
private Control control;



public static void main(String[] args) throws IOException {
    String name = "";
    String capital = "";
    String region = "";
    int region_Nbr = 0;
    int capital_population = 0;

    // TODO code application logic here
    String filename = "Countries.txt";
    String inputString;

    FileInputStream fis1 = new FileInputStream(filename);
    BufferedReader br1 = new BufferedReader(new InputStreamReader(fis1));
    inputString = br1.readLine();
    int count = 0;
    while (inputString != null) {
        //System.out.print(inputString + "\n");

        name = inputString.substring(0, 13).trim();
        //System.out.print(name + ", "); //echo

        capital = inputString.substring(24, 36).trim();
        //System.out.print(capital + ", ");//echo

        region = inputString.substring(40, 56).trim();
        //System.out.print(region + ", "); //echo

        region_Nbr = Integer.parseInt(inputString.substring(64, 66).trim());
        //System.out.print(region_Nbr + ", ");//echo

        capital_population = Integer.parseInt(inputString.substring(72, inputString.length()).trim());
        //System.out.print(capital_population + "\n");

        countryInfo[count] = new Country(name, capital, region, region_Nbr, capital_population);
        //Control.printArray(countryInfo);
        inputString = br1.readLine();

        count++;

    } //end while
    br1.close();

    System.out.println(Arrays.toString(countryInfo));
  }

}// end class Main




import java.util.Arrays;


public class Country
{

    private String name;
    private String capital;
    private String region;
    private int region_Nbr;
    private int capital_population;
    private Control control;


    public Country (String strName, String strCapital,String strRegion, int iregion_Nbr,     int icapitalpop)
    {
        name = strName;
        capital = strCapital;
        region = strRegion;
        region_Nbr = iregion_Nbr;
        capital_population = icapitalpop;
       // System.out.println ("object is: " + name + " " + capital + " " + region + " " + region_Nbr + " " + capital_population);


    }// end constructor




}//end class




}//end class
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
NoobException
  • 616
  • 9
  • 20

2 Answers2

0

Every class has a toString method by default. You can override this method to return a more meaningful String value.

import java.util.Arrays;


public class Country
{
    private String name;
    private String capital;
    private String region;
    private int region_Nbr;
    private int capital_population;
    private Control control;

    public Country (String strName, String strCapital,String strRegion, int iregion_Nbr,     int icapitalpop)
    {
        name = strName;
        capital = strCapital;
        region = strRegion;
        region_Nbr = iregion_Nbr;
        capital_population = icapitalpop;
       // System.out.println ("object is: " + name + " " + capital + " " + region + " " + region_Nbr + " " + capital_population);      
    }// end constructor   

    public String toString() {
        return "Country: name = " + name + "; capital = " + capital + "; region = " + region + "; regionNbr = " + region_Nbr + "; population = " + capital_population;
    }
}//end class

Obviously you can format the rely any way you want

Updated with working example...

import java.io.IOException;
import java.util.Arrays;
import java.util.ResourceBundle.Control;

public class Main {

    private static Country[] countryInfo = new Country[1];

    public static void main(String[] args) throws IOException {
        new Main();
    }

    public Main() {

        String name = "";
        String capital = "";
        String region = "";
        int region_Nbr = 0;
        int capital_population = 0;

        countryInfo[0] = new Country("Australia", "Canberra", "AU", 61, 6000000);


        System.out.println(Arrays.toString(countryInfo));
    }

    public class Country {

        private String name;
        private String capital;
        private String region;
        private int region_Nbr;
        private int capital_population;
        private Control control;

        public Country(String strName, String strCapital, String strRegion, int iregion_Nbr, int icapitalpop) {
            name = strName;
            capital = strCapital;
            region = strRegion;
            region_Nbr = iregion_Nbr;
            capital_population = icapitalpop;
        }// end constructor

        @Override
        public String toString() {
            return "Country: name = " + name + "; capital = " + capital + "; region = " + region + "; regionNbr = " + region_Nbr + "; population = " + capital_population;
        }
    }//end class
}//end class

Outputs...

[Country: name = Australia; capital = Canberra; region = AU; regionNbr = 61; population = 6000000]
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

Since you want to print the elements of a Custom Class i.e Country in you case , you will need to override the toString implementation in the Country class. If you do not override the toString for a custom class, it will print you the reference .

@Override 
 public String toString() {
    return "Country: name = " + this.name + "; capital = " + this.capital + "; region = " + this.region ;
}

Why don't you use this

List<Country> countryInfoList = new ArrayList<Country>();
.....
and then just add the country to the countryInfoList i.e countryInfoList.add(country);

p.s: in both cases, you will have to override the toString implementation.

Durgadas Kamath
  • 400
  • 2
  • 12