My Text file is below. The bounds should be 0-2 for letter A or D then 2-15 for StateName(these are new states being added to a stack) and 15-25 for what should be the capital, but not all of them have capitals and for those I am getting an out of bounds exception is there a way around this or to tell it to stop then go to the next line when it might detect it out of bounds without a try catch.

D Pennsylvania
D PA
A PuertoRico   San Juan
A Maine        Augusta
D Tennessee
D Franklin
A Alabama      Montgomery
A Thailand     Bangkok

My code is

public void loadStacks(String filename) throws IOException 
    {
        FileInputStream fis1 = new FileInputStream("UpdateStacks.txt"); 
        //obtains file statearray.txt
        BufferedReader br1 = new BufferedReader(new InputStreamReader(fis1)); 
        //reads statearray.txt
        String stackLetter, stateName, stateCapital;

        String inputString;
        inputString = br1.readLine();

        while (inputString != null) 
            //while loop to continually create state objects
        {
            stackLetter = inputString.substring(0, 2).trim(); 
            //parses the file for Stack Letter A or D which is add or delete
            stateName = inputString.substring(2, 14).trim(); 
            //parses the file for state name
            stateCapital = inputString.substring(14, 15).trim(); 
            //parses the file for state capital

         if(stackLetter.equals("D"))
             deleteStack.push(
                     new States(inputString.substring(1),"", "",0,"",0));
         else
             addStack.push(
                     new States(inputString.substring(1),"", "",0,"",0));

            inputString = br1.readLine();
        } // end while (inputString != null) 
        br1.close();
    }//end loadData(String filename) throws IOException 

maybe you should check first the string's length before using the substring method to avoid the OutOfBounds exception. :)

you must readline then split the line by spaces, count splited data, then know how many data splitted, then insert into the corresponding variables. with this you will always know the amount of data needed to insert.

Also why use substring if you can not determing the data. Use regular expression. You can do all in 5lines of code i think.

String st  = "D foo New Yrk"; // foo.readline()
String del =" ";          // split delimeter
String [] al = st.split(del); // you got that splitted
// note that to get the values that you need to use
for (int x=0;x< al.length;x++){

         System.out.println(al[x]);
        }

got the idea?

Splitting on blanks is not such a good idea.
The file format is defined by columns, so that's how you should parse it. Splitting on blanks will fail if any of the data fields contain an embedded blank, eg San Juan on line 3.
The best answer is to substring according to the file spec, but test the length of the line before substringing the capital.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.