Hello, I'm new to Stack Overflow, and this is my first question/post! I'm working on a project for school using Java. The first part I'm having trouble with inolves:
- Read each line in a file (listed at the end of my post) one time
- Create a "ragged" array of integers, 4 by X, where my 4 rows will be the "region number" (the number found in the Nbr of Region) column, and fill each column with the state population for that region.
So, for example, Row 1 would hold the state populations of Region 1 resulting in 6 columns, Row 2 represents Region 2 resulting in 7 columns, and so on resulting in a "ragged" array.
My question is how to populate, or what would be the best way to populate my array with the results of my file read? I know how to declare the array, initialize the array and create space in the array, but I'm not sure of how to write my method in my State class to populate the results of my file read into the array. Right now I'm getting an "out of bounds" error when I try to compile this code using Netbeans.
Here is my code for Main and State. my input file is listed beneath it:
import java.util.*;
import java.io.*;
public class Main
{
public static void main(String[] args) throws IOException
{
// create new jagged array obj and fill it with some
// initial "dummy" values
int[][] arrPopulation =
{
{0,1,2,3,4,5},
{0,1,2,3,4,5,6},
{0,1,2,3,4,5,6,7,8,9},
{0,1,2,3,4,5,6,7,8,9,10}
};//end array declaration
// read in file States.txt, instantiate BufferedReader object,
// set new BufferedReader object to variable @newLine
FileReader f = new FileReader("States.txt");
BufferedReader br = new BufferedReader(f);
String newLine = br.readLine();
for (int rows = 0; rows < arrPopulation.length; rows++)
{
for (int col = 0; col < arrPopulation[col].length; col++) {
System.out.print(arrPopulation[rows][col] + " ");
}
// display on new lines; print out in a "table" format
System.out.println();
} // end for
State newState = new State(newLine);
int count = 0;
while(newLine != null)
{
newLine = br.readLine();
System.out.println(newState.getRegionNum());
}// end while
br.close();//close stream
} // end public static void main
} // end main
This is what I have for my State class so far:
import java.util.*;
public class State
{
private String statePop, stateNum, regionNum;
public State(String fileRead)
{
statePop = fileRead.substring(32,39);
regionNum = fileRead.substring(55,fileRead.length());
} // end constructor
public int getStatePop()
{
int population = Integer.parseInt(statePop);
return population;
} // @method getStatePop end method
public int getRegionNum()
{
int numOfRegion = Integer.parseInt(regionNum);
return numOfRegion;
}// end getRegionNum
public int getAvgPop()
{
int average = 2+2;
return average;
// total number of populations
// divide number of populations
}// @return the average population of states
public int getStateTotal()
{
//initialize static variable
int totalPopulation = 0;
int stateTotal = this.getStatePop() + totalPopulation;
return stateTotal;
} // @return stateTotal
public String toString()
{
return statePop + " " + stateNum + " ";
} // @method end toString method
} // end State class
The names of the columns (not used in the file read, just for explaining purposes) are:
State Capital Abbrev Population Region Nbr of Region
Washington Olympia WA 5689263West 6
Oregon Salem OR 3281974West 6
Massachusetts Boston MA 6147132New_England 1
Connecticut Hartford CT 3274069New_England 1
Rhode_Island Providence RI 988480New_England 1
New_York Albany NY18146200Middle_Atlantic2
Pennsylvania Harrisburg PA12001451Middle_Atlantic2
New_Jersey Trenton NJ 8115011Middle_Atlantic2
Maryland Annapolis MD 5134808Middle_Atlantic2
West_Virginia Charleston WV 1811156Middle_Atlantic2
Delaware Dover DE 743603Middle_Atlantic2
Virginia Richmond VA 6791345Middle_Atlantic2
South_Carolina Columbia SC 3835962South 3
Tennessee Nashville TN 5430621South 3
Maine Augusta ME 1244250New_England 1
Vermont Montpelier VT 588632New_England 1
New_Hampshire Concord NH 1185048New_England 1
Georgia Atlanta GA 7642207South 3
Florida Tallahassee FL14915980South 3
Alabama Montgomery AL 4351999South 3
Arkansas Little_Rock AR 2538303South 3
Louisiana Baton_Rouge LA 4368967South 3
Kentucky Frankfort KY 3936499South 3
Mississippi Jackson MS 2752092South 3
North_Carolina Raleigh NC 7546493South 3
California Sacramento CA32182118West 6
Idaho Boise ID 1228684West 6
Montana Helena MT 880453West 6
Wyoming Cheyenne WY 480907West 6
Nevada Carson_City NV 1746898West 6
Utah Salt_Lake_City UT 2099758West 6
Colorado Denver CO 3970971West 6
Alaska Juno AK 614010West 6
Hawaii Honolulu HI 1193001West 6
Am I on the right track here?
Thanks for any help in advance, and sorry for the long post!