3 Replies - 687 Views - Last Post: 18 June 2012 - 08:17 PM Rate Topic: -----

#1 AllHighway  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 08-February 12

Printing an array of objects in columns.

Posted 17 June 2012 - 10:33 PM

I am trying to print an array out with several objects that contain five attributes. I am attempting to use the System.out.format function to align the attributes into five different columns. This is the code I am trying to work with.
    public void display()
      {
          String format = "%1s %n";
          String name="Country";//sets country to print in the header
          String capital="Capital";//sets captial to print in the header
          String region="Region";//sets regiong to print in the header
          String regionNmb= "Region Number";//sets region number to orint in the header
          String pop="Capital Population";//sets catiptial population to print in the header
         
          System.out.printf ("%-27s %-30s %-23s %-17s %-20s %n %n %n",name, capital,region,regionNmb,pop);//header
          for (int i=0; i < 43; i++)//for loop to print out array of countrues
              System.out.format(String.format(format, (Object[])country));//country is an array
              //System.out.println(country[i]);           
              //System.out.println("");
      }


This is the code where my attributes are located.
package project1barry;


public class CountryData 
{
 private String countryName;//sets the strings and integers 
 private String countryCapital;
 private String countryRegion;
 private int countryRegionNumber;
 private int countryPopulation;
 
 public CountryData
 (String countryName, String countryCapital, String countryRegion, int countryRegionNumber, int countryPopulation)
 {
    this.countryName =  countryName;//reference to current object
    this.countryCapital = countryCapital;//reference to current object
    this.countryRegion = countryRegion;//referenceto current object
    this.countryRegionNumber = countryRegionNumber;//reference to current object
    this.countryPopulation = countryPopulation;//reference to current object
 }
 
 public String getCountryName()
 {
     return countryName;// returns value from the method
 }
 public String getCountryCapital()
 {
     return countryCapital;
 }
 public String getCountryRegion()
 {
     return countryRegion;
 }
 public int getCountryRegionNumber() 
 {
     return countryRegionNumber;
 }
 public int GetCountryPopulation()
 {
     return countryPopulation;
 }
 public String toString()
 {
     String s = countryName + "\t\t\t" + countryCapital + "\t\t\t" + countryRegion + "\t\t\t" + countryRegionNumber + "\t\t "+ countryPopulation;
     return  s;
 }  
}   //Closes CountryData Class
 
 



Any help is greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Printing an array of objects in columns.

#2 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 286
  • View blog
  • Posts: 1,619
  • Joined: 15-May 08

Re: Printing an array of objects in columns.

Posted 18 June 2012 - 12:19 AM

Could you please post a sample output of how it looks, perhaps just the first 5 countries
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Printing an array of objects in columns.

Posted 18 June 2012 - 03:52 AM

I'll stop right here

System.out.printf ("%-27s %-30s %-23s %-17s %-20s %n %n %n",name, capital,region,regionNmb,pop);//header

You have 8 %xxx in your format but you pass on 5 elements to print to printft()
Was This Post Helpful? 1
  • +
  • -

#4 AllHighway  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 08-February 12

Re: Printing an array of objects in columns.

Posted 18 June 2012 - 08:17 PM

Thanks a lot. I got it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1