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.

New Topic/Question
Reply




MultiQuote





|