Question
I need help with a homework assignment. My job is to read in two separate text files containing data. The first text file called statecode.txt
I need help with a homework assignment. My job is to read in two separate text files containing data. The first text file called statecode.txt is simply in the format of an integer followed by a state name
EX:
1 Alabama
2 Alaska
The second file is accidents.txt, a text file containing "accident data" about each state. The format of this text file is 4 integers per line each indicating a different data type. The first integer on the line represents the state code from the previous file, the second integer is the total vehicles involved in the accident, the third integer is the people involved in the accident, and the fourth integer is the fatalities from the accident. This file is several hundred lines long.
My task is to create an array list, read in the state ID and create an object for each state and add those objects into the array list, read the data from "accidents.txt" and count the total amount of accidents at each state and add that to the array list, and the output this data using the toString() method.
I have written most of the code, but I am facing a roadblock and I can't figure out the answer. When I'm trying to output my text, it's outputting 0 NULL 0, instead of outputting the State ID, State name, and total amount of accidents. I'm guessing there's a good amount of errors in my code, but I don't know where they are, as I am fairly new to java.
I would greatly appreciate if you could look over my code and tell me where my error is and what I should do to correct it.
*******OuterMain.java******
import java.util.*; import java.io.*; public class OuterMain {
public static void main(String[] args) throws FileNotFoundException{ String stateName = ""; int i = 0; int vehicle = 0; int persons = 0; int fatals = 0; int accidentCount = 0; int stateID = 0; StateAccident newState = new StateAccident (stateID, stateName, accidentCount); ArrayList
while(stateCodeFile.hasNext()) { stateCodeFile.nextLine(); stateID = stateCodeFile.nextInt(); newState.setStateID(stateID); stateName = stateCodeFile.nextLine(); newState.setStateName(stateName); stateData.add(new StateAccident(stateID, stateName));
} stateCodeFile.close();
while(stateAccidentFile.hasNext()) { stateAccidentFile.nextLine(); i = stateAccidentFile.nextInt(); vehicle = stateAccidentFile.nextInt(); persons = stateAccidentFile.nextInt(); fatals = stateAccidentFile.nextInt(); newState.setAccidentCount(accidentCount); stateData.add(new StateAccident(accidentCount));
if (stateID == i) { newState.incrementAccidentCount();
} Iterator itr=stateData.iterator();
while(itr.hasNext()){ StateAccident st=(StateAccident)itr.next(); System.out.println(st.getStateID()+" "+st.getStateName()+" " +st.getAccidentCount()); }
} } }
******StateAccident.java*********
import java.util.*; public class StateAccident {
private int stateID; private String stateName; private int accidentCount;
public StateAccident(int stateID, String stateName){ stateID = 0; stateName = "unknown"; } public StateAccident() { } public StateAccident(int stateID, String stateName, int accidentCount) { stateID = 0; stateName = "unknown"; accidentCount = 0; } public StateAccident(int accidentCount) { }
public void setStateID(int StateID) { this.stateID = stateID; } public int getStateID() { return stateID; } public void setStateName(String stateName) { this.stateName = stateName; } public String getStateName() { return stateName; } public void incrementAccidentCount() { accidentCount++; } public void setAccidentCount(int accidentCount) { this.accidentCount=accidentCount;
}
public int getAccidentCount() { return accidentCount; } public String toString() { return ("StateID:"+this.getStateID()+ " State Name: "+ this.getStateName() + " Accident Count: "+ this.getAccidentCount()); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started