12 Replies - 919 Views - Last Post: 19 September 2012 - 01:50 PM Rate Topic: -----

#1 scy0846  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 03-October 11

Creating multiple ArrayLists from an Array of Objects

Posted 13 September 2012 - 12:43 PM

Ok so I am creating an array of objects from a .txt file. The objects have attributes stateName, stateCapital, stateAbbreviation, statePopulation, stateRegion, RegionNumber.

I am supposed to sort these objects based off of the region number. I tried a couple ways, but for some reason they all keep giving me an array or True or False values and not the objects themselves. I tried a compareTo method as well, but it kept giving me an int cannot be deferenced.

my code for StateController.


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package project2mitchell;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

/**
 *
 * @author Taylor
 */
public class StateController 
{
    private States[] myStates;
    private int numStates = 0;
    public int swapCount;
    private boolean swap;

    public StateController(int maxSize) 
    {
        myStates = new States[maxSize];
    } // end stateController()
    
    
    /**
     * Routine for loading data into the array of States from "statesarray2.txt".
     *
     * @param filename 
     * @throws IOException
     * @param name
     * @param cap
     * @param abbr
     * @param pop
     * @param reg
     * @param regnum
     * @return array of States objects from statesarray2.txt 
     */
    public void loadData(String filename) throws IOException 
    {
        FileInputStream fis1 = new FileInputStream("statesarray2.txt"); //obtains file statearray.txt
        BufferedReader br1 = new BufferedReader(new InputStreamReader(fis1)); //reads statearray.txt
        int regnum, pop;
        String name, cap, abbr, reg;

        String inputString;
        inputString = br1.readLine();

        while (inputString != null) //while loop to continually create state objects
        {
            name = inputString.substring(0, 15).trim(); //parses the file for name
            cap = inputString.substring(15, 30).trim(); //parses the file for capital
            abbr = inputString.substring(30, 32).trim(); //parses the file for state abbreviation
            pop = Integer.parseInt(inputString.substring(36, 40).trim()); //parses the file for population
            reg = inputString.substring(40, 55).trim(); //parses the file for region
            regnum = Integer.parseInt(inputString.substring(55, 56).trim()); //parses the file for region number

            myStates[numStates] = new States(name, cap, abbr, pop, reg, regnum); //creates a new state object with attributes
            numStates++; //increases state count
            inputString = br1.readLine();
        } // end while (inputString != null) 
        br1.close();
    }//end loadData(String filename) throws IOException
    
    /**
     * Routine for displaying state objects.
     * 
     * @param index
     * @return myStates[index]
     */
    public void displayStates() 
    {
        int index; //index is a local variable for state objects

        for (index = 0;
                index < numStates;
                index++) //for loop to continue making state objects stopping at numStates(50)
        {
            System.out.println(myStates[index]); //displays state objects(unsorted)
        }//end for loop
    }//end displayStates()
    
    public void ArrayList()
    {
        
    ArrayList regionOne = new ArrayList();
    ArrayList regionTwo = new ArrayList();
    ArrayList regionThree = new ArrayList();
    ArrayList regionFour = new ArrayList();
    ArrayList regionFive = new ArrayList();
    ArrayList regionSix = new ArrayList();
    
    int index;
    
         for (index = 0;
                index < (numStates - 1);
                index++)
         {
            regionOne.add(myStates[index].getRegionNumber()==1);
         }
         for (index = 0;
                index < (numStates - 1);
                index++)
         {
            regionTwo.add(myStates[index].getRegionNumber()==2);
         }
         for (index = 0;
                index < (numStates - 1);
                index++)
         {
            regionThree.add(myStates[index].getRegionNumber()==3);
         }
         for (index = 0;
                index < (numStates - 1);
                index++)
         {
            regionFour.add(myStates[index].getRegionNumber()==4);
         }
         for (index = 0;
                index < (numStates - 1);
                index++)
         {
            regionFive.add(myStates[index].getRegionNumber()==5);
         }
         for (index = 0;
                index < (numStates - 1);
                index++)
         {
            regionSix.add(myStates[index].getRegionNumber()==6);
         }
         System.out.println(regionOne + "\n" + regionTwo + "\n" + regionThree + "\n" + regionFour + "\n" + regionFive + "\n" + regionSix);
    }
}


My Main class implements the drivers and that is working just fine.

If you need the State class to identify how that is made...
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package project2mitchell;

/**
 *
 * @author Taylor
 */
public class States 
{
    private int regionNumber;
    private int statePopulation;
    private String stateName;
    private String stateCapital;
    private String stateAbbreviation;
    private String stateRegion;

    
    /**
     * 
     * @param name
     * @param cap
     * @param abbr
     * @param pop
     * @param reg
     * @param regNum 
     * @returns State object
     */
    public States(String name, String cap, String abbr, int pop, String reg, int regNum) 
    {
        stateName = name;
        stateCapital = cap;
        stateAbbreviation = abbr;
        statePopulation = pop;
        stateRegion = reg;
        regionNumber = regNum;

    }// end State

    /**
     *
     * @return stateName
     */
    public String getStateName() 
    {
        return stateName;
    }//end getStateName

    /**
     *
     * @return stateCapital
     */
    public String getStateCapital() 
    {
        return stateCapital;
    }//end getStateCapital

    /**
     *
     * @return stateAbbreviation
     */
    public String getStateAbbreviation() 
    {
        return stateAbbreviation;
    }//end getStateAbbreviation

    /**
     *
     * @return stateRegion
     */
    public String getStateRegion() 
    {
        return stateRegion;
    }//end getStateRegion

    /**
     *
     * @return regionNumber
     */
    public int getRegionNumber() 
    {
        return regionNumber;
    }//end getRegionNumber

    /**
     *
     * @return statePopulation
     */
    public int getStatePopulation() 
    {
        return statePopulation;
    }//end getStatePopulation()

    /**
     * @param str
     * @return toString State Object
     */
    @Override
    public String toString() 
    {
        String str = String.format("%15s %15s %5s %12d %17s %5d",
                stateName, stateCapital, stateAbbreviation, statePopulation, stateRegion, regionNumber);
        return str;
    }// end toSTring()
}// end State() constructor




Any Ideas? I was told not to sort them, but just separate them by regionNumber. The region numbers are values 1-6.

Is This A Good Question/Topic? 0
  • +

Replies To: Creating multiple ArrayLists from an Array of Objects

#2 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: Creating multiple ArrayLists from an Array of Objects

Posted 13 September 2012 - 12:51 PM

You were right to think compareTo, but what you should have done is this:
public class State implements Comparable<State>
{
  @Override
  public int compareTo( State st )
  {
     //logic to compare
  }
}


the int you return is determined like so:
Value                   Meaning 
----------------------------------------------------------
Less than zero          The invoking State is less than st.
Greater than zero	The invoking State is greater than st. 
Zero                    The two strings are equal.

This post has been edited by SwiftStriker00: 13 September 2012 - 12:52 PM

Was This Post Helpful? 0
  • +
  • -

#3 scy0846  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 03-October 11

Re: Creating multiple ArrayLists from an Array of Objects

Posted 13 September 2012 - 01:02 PM

I still would keep the ArrayList method the same right? I am required to use that class.
Was This Post Helpful? 0
  • +
  • -

#4 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: Creating multiple ArrayLists from an Array of Objects

Posted 13 September 2012 - 01:09 PM

You can, I was only giving you suggestions on how to modify your States class in order to get compareTo to work propperly.

Now I think you should be using 2d arrays/arraylists, but thats another argument for another time :)
Was This Post Helpful? 0
  • +
  • -

#5 scy0846  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 03-October 11

Re: Creating multiple ArrayLists from an Array of Objects

Posted 13 September 2012 - 01:16 PM

I'll look at those I'm fairly new to object arrays. Thank you for your help
Was This Post Helpful? 0
  • +
  • -

#6 scy0846  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 03-October 11

Re: Creating multiple ArrayLists from an Array of Objects

Posted 13 September 2012 - 01:22 PM

public int compareTo(States region)
    {
        int reg = this.regionNumber.compareTo(region.regionNumber);
        return reg;
    }


I am getting an int cannot be dereferenced again. This should be the correct way to implement it according to every place I've looked.

The class now states at the top
public class States implements Comparable<States>

Was This Post Helpful? 0
  • +
  • -

#7 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: Creating multiple ArrayLists from an Array of Objects

Posted 13 September 2012 - 01:27 PM

Your over complicating it. Let me break it up for you:

public int compareTo( States region )
{
  int invokingRegionNumber = this.regionNumber;
  int paramRegionNumber = region.regionNumber;
  
  if( invokingRegionNumber < paramRegionNumber )
  {
    //return an int
  }else if( invokingRegionNumber > paramRegionNumber )
  {
    //return an int
  }else
  {
     // invoking == param
    //return an int
  }
}


You should read the JavaDocs for this as well!

This post has been edited by SwiftStriker00: 13 September 2012 - 01:28 PM

Was This Post Helpful? 0
  • +
  • -

#8 scy0846  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 03-October 11

Re: Creating multiple ArrayLists from an Array of Objects

Posted 13 September 2012 - 01:31 PM

reading the Javadocs now I appreciate it
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

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

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Creating multiple ArrayLists from an Array of Objects

Posted 13 September 2012 - 01:36 PM

You don't even need a if :)
public int compareTo(States region)
    {
        return this.regionNumber - region.regionNumber;
    }


Was This Post Helpful? 1
  • +
  • -

#10 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: Creating multiple ArrayLists from an Array of Objects

Posted 13 September 2012 - 01:40 PM

View Postpbl, on 13 September 2012 - 04:36 PM, said:

You don't even need a if :)
public int compareTo(States region)
    {
        return this.regionNumber - region.regionNumber;
    }


Very true, I figured breaking it up would help explain the concept better. Believe me it was arduous writing that entire if/else block for int comparison :)
Was This Post Helpful? 1
  • +
  • -

#11 scy0846  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 03-October 11

Re: Creating multiple ArrayLists from an Array of Objects

Posted 13 September 2012 - 06:01 PM

I placed

public int compareTo(States region)
    {
        return this.regionNumber - region.regionNumber;
    }

in States class

And I changed my code in my StateController class. To:

   public void ArrayList()
    {
        
    ArrayList regionOne = new ArrayList();
    ArrayList regionTwo = new ArrayList();
    ArrayList regionThree = new ArrayList();
    ArrayList regionFour = new ArrayList();
    ArrayList regionFive = new ArrayList();
    ArrayList regionSix = new ArrayList();
    
    int index;
    
         for (index = 0;
                index < (numStates - 1);
                index++)
         {
            regionOne.add(myStates[index].getRegionNumber().compareTo(1));
         }
         for (index = 0;
                index < (numStates - 1);
                index++)
         {
            regionTwo.add(myStates[index].getRegionNumber().compareTo(2));
         }
         for (index = 0;
                index < (numStates - 1);
                index++)
         {
            regionThree.add(myStates[index].getRegionNumber().compareTo(3));
         }
         for (index = 0;
                index < (numStates - 1);
                index++)
         {
            regionFour.add(myStates[index].getRegionNumber().compareTo(4));
         }
         for (index = 0;
                index < (numStates - 1);
                index++)
         {
            regionFive.add(myStates[index].getRegionNumber().compareTo(5));
         }
         for (index = 0;
                index < (numStates - 1);
                index++)
         {
            regionSix.add(myStates[index].getRegionNumber().compareTo(6));
         }
         System.out.println(regionOne + "\n" + regionTwo + "\n" + regionThree + "\n" + regionFour + "\n" + regionFive + "\n" + regionSix);
    }


I've also tried...
for (index = 0;
                index < (numStates - 1);
                index++)
         {
            regionOne.add(myStates[index].getRegionNumber().compareTo(States 1));
         }


and
for (index = 0;
                index < (numStates - 1);
                index++)
         {
            regionOne.add(myStates[index].getRegionNumber().compareTo(region.1));
         }


I keep getting the error "int cannot be dereferenced."
I have no idea why that is.
It looks like it should work and I've googled the error, but I can't find a resolution for it.
I cannot figure out what I must be missing
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

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

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Creating multiple ArrayLists from an Array of Objects

Posted 14 September 2012 - 03:49 AM

regionOne.add(myStates[index].getRegionNumber().compareTo(1));

you cannot compareTo() a basic datatype like a int
assuming getRegionNumber() returns a int like 10 you cannot

10.compareTo(1)

you have to compareTo() two region
Was This Post Helpful? 0
  • +
  • -

#13 scy0846  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 03-October 11

Re: Creating multiple ArrayLists from an Array of Objects

Posted 19 September 2012 - 01:50 PM

I changed the code to this. For some reason nothing is printing out at all, but I think that's because I messed up something in the parsing code which I will fix. I was just curious if this might be what you meant. It's not giving me any compilation errors.


public void ArrayList()
    {
        
    ArrayList regionOne = new ArrayList();
    ArrayList regionTwo = new ArrayList();
    ArrayList regionThree = new ArrayList();
    ArrayList regionFour = new ArrayList();
    ArrayList regionFive = new ArrayList();
    ArrayList regionSix = new ArrayList();
    
    int index=0;
    Integer one=myStates[index].getRegionNumber();
            
         for (index = 0;
                index < (numStates - 1);
                index++)
         {
           regionOne.add(one.compareTo(1));
         }
         for (index = 0;
                index < (numStates - 1);
                index++)
         {
            regionTwo.add(one.compareTo(2));
         }
         for (index = 0;
                index < (numStates - 1);
                index++)
         {
            regionThree.add(one.compareTo(3));
         }
         for (index = 0;
                index < (numStates - 1);
                index++)
         {
            regionFour.add(one.compareTo(4));
         }
         for (index = 0;
                index < (numStates - 1);
                index++)
         {
            regionFive.add(one.compareTo(5));
         }
         for (index = 0;
                index < (numStates - 1);
                index++)
         {
            regionSix.add(one.compareTo(6));
         }
         System.out.println(regionOne + "\n" + regionTwo + "\n" + regionThree + 
                 "\n" + regionFour + "\n" + regionFive + "\n" + regionSix);
    }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1