I am trying to implement a Try/Catch to catch a NumberFormatException, but it is not working like I expected. It is catching the exception, but it won't let me use variables or any other data that did not have an error in it. Without the try/catch my program works if I delete the line that has the exception(so I don't get a java error and it stops). When my program runs with the try catch it doesn't produce any output because it won't work like the program should. It's reading text from a file and creating an object array from the file.
my try catch is supposed to catch the exception and print it on a file. and then I have to create my own exception, but I'm not sure how to implement it. I know the exception will use myStates[index].getRegionNumber()==2 as the error.
public class StateController
{
private States[] myStates;
private int numStates = 0;
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
{
try
{
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(32, 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();
}
catch (NumberFormatException exc)
{
String file = "errorfile.2.txt"; // name your output file
FileWriter fw = new FileWriter (file);
// creates object fw of type FileWriter.
BufferedWriter bw = new BufferedWriter (fw);
// creates object bw of type ...
PrintWriter outFile = new PrintWriter (bw);
// creates object outFile of type ..
outFile.println (exc); // writes this as a stream. Try it!
outFile.println ("BAD INPUT\nInvalid Population Value" );
// prints a blank line in the file
outFile.close();
}
catch ( ex2)
{
//create error exception here regionNumber cannot equal 2 or 4
}
} // end while (inputString != null)
br1.close();
}//end loadData(String filename) throws IOException
file is
Pennsylvania Harrisburg PA12001451Middle_Atlantic2 New_Jersey Trenton NJ 8115011Middle_Atlantic2 Maryland Annapolis MD 5134808Middle_Atlantic2 West_Virginia Charleston WV 18Q1156Middle_Atlantic2 Delaware Dover DE 743603Middle_Atlantic2 Virginia Richmond VA 67a1345Middle_Atlantic2 North_Carolina Raleigh NC 7546493South 3 South_Carolina Columbia SC 3835962South 3 Tennessee Nashville TN 5430621South 3 Maine Augusta ME 1244250New_England 1 Texas Austin TX19759614Southwest 5 New_Mexico Santa_Fe NM 1736931Southwest 5 Arizona Phoenix AZ 4668631Southwest 5 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 Vermont Montpelier VT 588632New_England 1 New_Hampshire Concord NH 1185048New_England 1 Georgia Atlanta GA 7642207South 3 Illinois Springfield IL12425326Midwest 4 Wisconsin Madison WI 5A23500Midwest 4 Minnesota St_Paul MN 4725419Midwest 4 Iowa Des_Moines IA 2862447Midwest 4 Missouri Jefferson_City MO 5438559Midwest 4 North_Dakota Bismark ND 638244Midwest 4 South_Dakota Pierre SD 738171Midwest 4 Nebraska Lincoln NE 1662719Midwest 4 Kansas Topeka KS 2629067Midwest 4 Oklahoma Oklahoma_City OK 3346713Southwest 5 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 Florida Tallahassee FL14915980South 3 Alabama Montgomery AL 4351999South 3 Mississippi Jackson MS 2752092South 3 Arkansas Little_Rock AR 2538303South 3 Louisiana Baton_Rouge LA 4368967South 3 Kentucky Frankfort KY 3936499South 3 Ohio Columbus OH 3346713Midwest 4 Michigan Lansing MI 9817242Midwest 4 Indiana Indianapolis IN 5899195Midwest 4

New Topic/Question
Reply




MultiQuote



|