简体   繁体   English

Java:希望忽略某些超出范围的行

[英]Java: Looking to ignore certain lines that are out of bounds

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: 目前,我正在研究一个程序,该程序分析文件并从每行中取出34-40个字符并将其放入数据库的表中,但是,每次遇到少于34个字符的行时,程序吐出此错误:

WARNING: General Error:String index out of range: 40 警告:常规错误:字符串索引超出范围:40

I'm still a novice at Java so a helping hand is desired. 我仍然是Java的新手,因此需要帮助。 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 一种方法是我现在尝试捕获字符34-40的方式,在下面的示例中将产生A00000

TMSS0000N-02:00:30 Moving Volume A00000 to NEXT TMSS0000N-02:00:30将音量A00000移至下一个

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]. 另一种方法是跳过所有其他行,仅查找其中包含“ MOVING VOLUME”的行(该行开头的时间戳发生更改,因此我认为这种方式会更困难)。

Would a simple IF statement somewhere in the FOR loop stop the error? 在FOR循环中某个地方的简单IF语句是否可以停止错误? For example: 例如:

if (cust.length >= 34) 
{
     cust.substring(34,40);
}
else 
{
     cust.substring = "XXXXXX";
}

When you call cust.substring(34,40); 当您调用cust.substring(34,40); , you need to make sure that cust.length >= 40 or it will throw the exception you get. ,您需要确保cust.length >= 40否则它将抛出您得到的异常。 See the javadoc for public String substring(int beginIndex, int endIndex) : 请参见javadoc以获取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. 抛出IndexOutOfBoundsException-如果beginIndex为负,或者endIndex大于此String对象的长度,或者beginIndex大于endIndex。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM