Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7820767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:25:27+00:00 2026-06-02T07:25:27+00:00

Currently I’m working on a program that parses through a file and takes characters

  • 0

Currently I’m working on a program that parses through a file and takes characters 34-40 out of each line and drops them into a table in a database, however each time I run into a line that is less than 34 characters long, my program spits this error out:

WARNING: General Error:String index out of range: 40

I’m still a novice at Java so a helping hand is desired. Here is a portion of my code:

try
            {
                fstream                             = new FileInputStream(filename);
                DataInputStream in                  = new DataInputStream(fstream);
                BufferedReader br                   = new BufferedReader(new InputStreamReader(in));
                PreparedStatement deleteFields      = null;
                String deleteTable                  = "DELETE FROM info WHERE VOLSER IS NOT NULL;";
                deleteFields                        = conn.prepareStatement(deleteTable);
                deleteFields.executeUpdate();
                deleteFields.close();
                logger.info(deleteTable);
                PreparedStatement updateFields      = null;
                String[] qmarks                     = new String[linenumber];

                //Adding data to the database
                for(int i = 0; i < linenumber; i++)
                {
                    String cust                 = in.readLine();
                    String subCust              = cust.substring(34,40);
                    qmarks[i]                   = subCust;
                    String updateString         = "REPLACE INTO info" + " (VOLSER) " + "VALUE ('" + (qmarks[i]) + "');";
                    logger.info(updateString);

                    try
                    {   
                        updateFields = conn.prepareStatement(updateString);
                        updateFields.executeUpdate();
                        updateFields.close();
                    }
                    catch (SQLException e)
                    {
                        logger.warning("Error:" + e.getMessage());
                    }
                }

This script would run daily so in order to purge yesterday’s data I have to DELETE all contents of the table and reinsert all values again. After deleting, it then starts to build an array of new contents. There are two ways I can approach this, and I’m looking for feedback from the community on which way is the best. One way is the way I’m doing to now and try to capture characters 34-40 which in the following example would yield A00000

TMSS0000N-02:00:30 Moving Volume A00000 to NEXT

The other way would be to skip all other lines and find only the lines that have “MOVING VOLUME” in them [the timestamp at the beginning of the line changes, so I’d figured this way would be more difficult].

Would a simple IF statement somewhere in the FOR loop stop the error? For example:

if (cust.length >= 34) 
{
     cust.substring(34,40);
}
else 
{
     cust.substring = "XXXXXX";
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T07:25:29+00:00Added an answer on June 2, 2026 at 7:25 am

    When you call cust.substring(34,40);, you need to make sure that cust.length >= 40 or it will throw the exception you get. See the javadoc for public String substring(int beginIndex, int endIndex):

    Throws IndexOutOfBoundsException – if the beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently i am working on 1 site, its requirement is that, i have to
Currently I am trying to get javascript code working that I didn't write (and
Currently, I am developing a product that does fairly intensive calculations using MS SQL
Currently, my MVC 3 app has a dependency on a static class that is
Currently, I have a log file of messages in one table in a MySQL
Currently, Enum.Parse supports only the comma as the value separator, so that MemberOne,MemberThree will
I want use html5's new tag to play a wav file (currently only supported
Currently, Tapping on the same Tab (in which user is working), The App moves
Currently I am trying to implement a timer class in VBA. For that purpose
Currently I have alot of information that are in several different divs. I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.