We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,459 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

How would I parse this file? I keep getting out of bounds

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 
4
Contributors
4
Replies
10 Hours
Discussion Span
7 Months Ago
Last Updated
6
Views
taylor.mitchell.353
Light Poster
39 posts since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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

dimasalang
Junior Poster in Training
76 posts since Nov 2010
Reputation Points: 11
Solved Threads: 12
Skill Endorsements: 2

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.

richieking
Master Poster
789 posts since Jun 2009
Reputation Points: 58
Solved Threads: 156
Skill Endorsements: 0
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?

richieking
Master Poster
789 posts since Jun 2009
Reputation Points: 58
Solved Threads: 156
Skill Endorsements: 0

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.

JamesCherrill
... trying to help
Moderator
8,530 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0962 seconds using 2.72MB