2017-04-11 54 views
0

我得到一個StringIndexOutOfBoundsException的線路閱讀(line.charAt(0) == 'I'),它說String index is out of range at 0當試圖評估文件輸入時StringIndexOutOfBoundsException?

我不確定這裏要做什麼。

QueueLinkedList<Customer> eventqueue = new QueueLinkedList<Customer>(); 
QueueLinkedList<Customer> customerqueue = new QueueLinkedList<Customer>(); 
int numberserved = 0, totalidletime = 0, longestbreak = 0, idletime = 0, linelength = 0, longestline = 0; 

try { 
    File file = new File("customersfile.txt"); 
    BufferedReader reader = new BufferedReader(new FileReader(file)); 

    String line; 
    while ((line = reader.readLine()) != null) { 
     //empty lines 
     while ((line = reader.readLine()) != "") { 
      //first line of file 
      while ((line = reader.readLine()) != "300") { 
       Customer newcust = new Customer(); 
       if (line.charAt(0) == 'I') { 
        newcust.id = line.charAt(11); 
       } 
       else { 
        newcust.arrtime = line.substring(14); 
        newcust.setarrtimesecs(newcust.arrtime); 
        newcust.setleavingtime(newcust.arrtime);  
       } 
       eventqueue.enqueue(newcust); 
      } 
     } 
    } 
}catch (IOException e) { 
    e.printStackTrace(); 
} 
+0

爲什麼使用reader.readLine())!= null爲什麼不只是hasNextLine(),我認爲你的方式會保持真實的空行 –

回答

0

此行爲空,索引0處沒有字符,則此字符串爲空。您正在執行這一行:

line.charAt(0) == 'I' 

嘗試這樣的方式,

if (line.trim().length() > 0 && line.charAt(0) == 'I') { 
0

在你的第二個while循環,你比較""之前需要trim

while ((line = reader.readLine().trim()) != "") { 

而且還與等號相比較,因爲!=運算符比較引用是否指向同一個對象。