线程" main"中的例外情况java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:-544

时间:2014-06-05 06:56:50

标签: java

我收到以下代码的StringIndexOutOfBoundsException。这是错误

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -544
    at java.lang.AbstractStringBuilder.substring(Unknown Source)
    at java.lang.StringBuffer.substring(Unknown Source)
    at com.sample.LogToExcel_Ver_4.getData(LogToExcel_Ver_4.java:89)
    at com.sample.LogToExcel_Ver_4.main(LogToExcel_Ver_4.java:131)

这是代码

    package com.sample;

  import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class LogToExcel_Ver_4 {
public static int stringTODate(String dt){
    Date date=null ; 
    try {  String str_date=dt;
     DateFormat formatter ; 
      formatter = new SimpleDateFormat("yy-MM-dd hh:mm:ss");
      date = (Date)formatter.parse(str_date);  
//   System.out.println("Today is " +date );
      } catch (ParseException e)
      {System.out.println("Exception :"+e);  }  

     return date.getDate();
}
public static void deleteAllData(){
    try {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection con = DriverManager.getConnection("jdbc:odbc:excel_ins", "", "");
 //       System.out.println("con:");
        Statement st = con.createStatement();
        int rs = st.executeUpdate("delete * from [Sheet1$]");
           System.out.println(rs+":deleted");

       st.close();
        con.close();

    } catch (Exception ex) {
        System.err.print("Exception: ");
        System.err.println(ex.getMessage());
    }
}
public static void insertData(Value_Ver_1 value){
    try {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        System.out.println("Connection1");
        Connection con = DriverManager.getConnection("jdbc:odbc:excel_ins", "", "");
  //      System.out.println("con:");
        System.out.println("Connection2");
        Statement st = con.createStatement();
        int rs = st.executeUpdate("insert into [Sheet1$] values('"+value.date+"','"+value.user+"','"+value.billingSystem+"','"+value.divisionId+"','"+value.accountNumber+"','"+value.billOrderNumber+"','"+value.billOrderType+"','"+value.JobId+"','"+value.viewable+"','"+value.reschedulable+"','"+value.cancelable+"','"+value.scheduleDate+"','"+value.scheduleTime+"','"+value.LOB+"','"+value.jobClass+"','"+value.cancelCode+"')");
  //         System.out.println(rs+":");
        System.out.println("Connection2");

       st.close();
        con.close();

    } catch (Exception ex) {
        System.err.print("Exception: ");
        System.err.println(ex.getMessage());
    }
}
public static Value_Ver_1 getData(String data){
    Value_Ver_1 value=new Value_Ver_1();
    StringBuffer sb=new StringBuffer();
    sb.append(data);
     String st=sb.substring(0,sb.indexOf(","));
     value.date=st.trim();
     st=sb.substring(sb.indexOf("[User:")+7, sb.indexOf("] [Class:"));
     value.user=st.trim();
     st=sb.substring(sb.indexOf("BillingSystem :- ")+"BillingSystem :- ".length(),    sb.indexOf(" | DivisionId"));
     value.billingSystem=st.trim();
     st=sb.substring(sb.indexOf("DivisionId :- ")+"DivisionId :- ".length(), sb.indexOf(" AccountNumber :-"));
     value.divisionId=st.trim();
     st=sb.substring(sb.indexOf("AccountNumber :-  ")+"AccountNumber :-  ".length(), sb.indexOf(" | BillOrderNumber :-"));
     value.accountNumber=st.trim();
     st=sb.substring(sb.indexOf("BillOrderNumber :- ")+"BillOrderNumber :- ".length(), sb.indexOf(" | BillOrderType :-"));
     value.billOrderNumber=st.trim();
     st=sb.substring(sb.indexOf("BillOrderType :- ")+"BillOrderType :- ".length(), sb.indexOf(" | JobId :-"));
     value.billOrderType=st.trim();
     st=sb.substring(sb.indexOf("JobId :- ")+"JobId :- ".length(), sb.indexOf(" | Viewable :-"));
     value.JobId=st.trim();
     st=sb.substring(sb.indexOf("Viewable :- ")+"Viewable :- ".length(), sb.indexOf(" | Reschedulable :-"));
     value.viewable=st.trim();
     st=sb.substring(sb.indexOf("Reschedulable :- ")+"Reschedulable :- ".length(), sb.indexOf(" | Cancelable :-"));
     value.reschedulable=st.trim();
     st=sb.substring(sb.indexOf("Cancelable :- ")+"Cancelable :- ".length(), sb.indexOf(" | ScheduleDate :-"));
     value.cancelable=st.trim();
     st=sb.substring(sb.indexOf("ScheduleDate :- ")+"ScheduleDate :- ".length(), sb.indexOf(" | ScheduleTime :-"));
     value.scheduleDate=st.trim();
     st=sb.substring(sb.indexOf("ScheduleTime :- ")+"ScheduleTime :- ".length(), sb.indexOf(" | LOB :-"));
     value.scheduleTime=st.trim();
     st=sb.substring(sb.indexOf("LOB :- ")+"LOB :- ".length(), sb.indexOf(" | JobClass :-"));
     value.LOB=st.trim();
     st=sb.substring(sb.indexOf("JobClass :- ")+"JobClass :- ".length(), sb.indexOf(" | CancelCode :-"));
     value.jobClass=st.trim();
     st=sb.substring(sb.indexOf("CancelCode :- ")+"CancelCode :- ".length());
     value.cancelCode=st.trim();
     return value;
     }
    /**
 * @param args
 */
public static void main(String[] args) throws Exception{
    FileInputStream fin=new FileInputStream("src/abc1.txt");
    StringBuffer sb=new StringBuffer();
    int ch;
    while ((ch = fin.read()) != -1)
        sb.append((char) ch);
      fin.close();
      sb.append("2014-");
      int index=sb.indexOf("2014-");
      int aindx=sb.indexOf("ScheduleDate :- ",index+5);
      int sindx=sb.indexOf("2014-",aindx+30);
      String dt=sb.substring(index, sindx);
 //     System.out.println(dt);
      Value_Ver_1 value=getData(dt);
      System.out.println("Started.......");
      System.out.println(value);
      System.out.println("Please wait until completed message u'll get.......");
   //   deleteAllData();
      insertData(value);
     while(true){
          index=sindx;
          aindx=sb.indexOf("ScheduleDate :- ",index+5);
          if(aindx<0){
              break;
          }
          sindx=sb.indexOf("2014-",aindx+30);
           dt=sb.substring(index, sindx);
           value=getData(dt);
          System.out.println(value);
          insertData(value);
      }
     System.out.println(".......Completed");

}
}

我正在阅读的文件是

2014-06-02 09:39:18,644 [INFO] [User: kkkkkkk] [Class: ViewScheduledAppointments] [Method: getViewScheduleJsonResponse] [TID: 1401721262542] [Msg: --  Appointment Detail --
 BillingSystem :- cccccccc | DivisionId :- cccccccc 
 AccountNumber :-  xxxxxxx | BillOrderNumber :- xxxxxxx | BillOrderType :- DW | JobId :- null | Viewable :- false | Reschedulable :- false | Cancelable :- false | ScheduleDate :- null | ScheduleTime :- null | LOB :- null | JobClass :- Not Applicable | CancelCode :- Not Applicable
2014-06-02 09:39:18,858 [INFO] [User: nehall129@hotmail.com] [Class: ViewScheduledAppointments] [Method: getViewScheduleJsonResponse] [TID: 1401721262447] [Msg: --  Appointment Detail --
 BillingSystem :- cccccccc | DivisionId :- cccccccc 
 AccountNumber :-  xxxxxx | BillOrderNumber :- xxxxxxxx | BillOrderType :- DW | JobId :- null | Viewable :- false | Reschedulable :- false | Cancelable :- false | ScheduleDate :- null | ScheduleTime :- null | LOB :- null | JobClass :- Not Applicable | CancelCode :- Not Applicable
2014-06-02 09:39:22,601 [INFO] [User: xxxxxxxxxx] [Class: ViewScheduledAppointments] [Method: getViewScheduleJsonResponse] [TID: 1401721307674] [Msg: --  Appointment Detail --
 BillingSystem :- cccccccc | DivisionId :- ddddd    
 AccountNumber :-  xxxxxxxxx | BillOrderNumber :- xxxxxxxxx | BillOrderType :- SR | JobId :- null | Viewable :- false | Reschedulable :- false | Cancelable :- false | ScheduleDate :- null | ScheduleTime :- null | LOB :- null | JobClass :- Not Applicable | CancelCode :- Not Applicable
 AccountNumber :-  xxxxxxxxx | BillOrderNumber :- xxxxxxxxx | BillOrderType :- UP | JobId :- null | Viewable :- true | Reschedulable :- true | Cancelable :- false | ScheduleDate :- 2014-06-05 | ScheduleTime :- 08:00-12:00 | LOB :- ["D"] | JobClass :- Not Applicable | CancelCode :- Not Applicable
 2014-06-02 09:39:36,007 [INFO] [User: xxxxxxx] [Class: ViewScheduledAppointments] [Method: getViewScheduleJsonResponse] [TID: 1401720018745] [Msg: --  Appointment Detail --
 BillingSystem :- xxxxxxxx | DivisionId :- ddddddd  
 AccountNumber :-  xxxxxxxx | BillOrderNumber :- 1000856216501125 | BillOrderType :- S | JobId :- 10008562165011250001 | Viewable :- true | Reschedulable :- false | Cancelable :- false | ScheduleDate :- 2014-06-02 | ScheduleTime :- 09:00-19:00 | LOB :- ["C","N"] | JobClass :- S | CancelCode :- null

所需的输出应该是excel

日期&amp;时间用户名BillingSystem DivisionId AccountNumber BillOrderNumber BillOrderType JobId可查看可重新计划可取消ScheduleDate ScheduleTime LOB JobClass CancelCode

按此顺序

2 个答案:

答案 0 :(得分:1)

这是|分隔数据,所以不是这个非常凌乱的代码,为什么不简单地使用

String arr [] = data.split ("\\|");

答案 1 :(得分:0)

负值总是超出范围,因为最低可能值为0