Results 1 to 2 of 2
  1. #1
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

    Default Question with bubbleSort()

    My question is where do I call the bubbleSort() to sort an array of country objects by country name. I tried it in Main but got errors.

    This is where I made the bubbleSort()
    Java Code:
    public class Sort
    { 
        Country[] myCountries = new Country[43];
    
        public void bubbleSort()
        {
            int out, in;
    
            for (out=myCountries.length - 1; out>1; out--)
                for (in=0; in<out; in++)
                    if( myCountries[in].getCountryName().compareTo(myCountries[in+1].getCountryName())<0 )
                        swap (in, in+1);
        }//end bubbleSort()
    
        private void swap(int one, int two)
        {
            Country temp = myCountries[one];
            myCountries[one] = myCountries[two];
            myCountries[two] = temp;
        }//end swap()
    }//end Sort
    And this is my Main class
    Java Code:
    public class Main
    {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) throws java.io.IOException
        {
            FileInputStream fis1 = new FileInputStream("AsiaCountries.Fall2010.txt");
            BufferedReader br1 = new BufferedReader(new InputStreamReader(fis1));
    
            String inline;
            int totalReg1Counter = 0, totalReg2Counter = 0, totalReg3Counter = 0;
            int totalReg4Counter = 0, index = 0;
            Country[] myCountries;
            myCountries = new Country[43];
    
    
            while ((inline = br1.readLine()) != null)
            {
                myCountries[index] = new Country(inline);
                index++;
            } // end while
            
            for(int count=0; count < index; count++)
            {
                System.out.println(myCountries[count]);
            }//end for
    
            myCountries.bubbleSort();
        }//end main()
    }//end Main
    This is my Country class
    Java Code:
    public class Country 
    {
        private String countryName, countryCapital, countryRegion, countryAbbrev;
        private int countryRegionNum;
        public static int countryObjCounter;
        
        public Country(String countryInput)
        {
            countryName = countryInput.substring(0,17).trim();
            countryCapital = countryInput.substring(21,40).trim();
            countryRegion = countryInput.substring(44,55).trim();
            countryRegionNum = Integer.parseInt(countryInput.substring(63,65).trim());
            countryAbbrev = countryInput.substring(66,68).trim();
            countryObjCounter++;
    
        }//end parseData Constructor()
    
        public String getCountryName()
        {
            return countryName;
        }//end getCountryName()
    
        public String getCountryCapital()
        {
            return countryCapital;
        }//end getCountryCapital()
    
        public String getCountryRegion()
        {
            return countryRegion;
        }//end getCountryRegion()
    
        public int getCountryRegionNum()
        {
            return countryRegionNum;
        }//end getCountryRegionNum()
    
        public String getCountryAbbrev()
        {
            return countryAbbrev;
        }//end getCountryAbbrev()
        
        public String toString()
        {
            return String.format("%-23s %-30s %-24s  %-16d %-4s\n",
            countryName, countryCapital,countryRegion, countryRegionNum,
            countryAbbrev);
    
        }//end toString()
    }//end Country
    Last edited by Metastar; 09-13-2010 at 06:09 PM.

  2. #2
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

Similar Threads

  1. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 09:49 AM
  2. MVC question
    By Tom_ in forum Advanced Java
    Replies: 1
    Last Post: 08-12-2010, 01:01 PM
  3. help question
    By 99w210 in forum NetBeans
    Replies: 0
    Last Post: 03-08-2010, 06:23 AM
  4. Die Bubblesort
    By N3VRMND in forum New To Java
    Replies: 2
    Last Post: 11-06-2009, 04:44 PM
  5. Question
    By ayoood in forum New To Java
    Replies: 16
    Last Post: 05-21-2008, 03:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •