Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 尝试计算文件输入时是否存在StringIndexOutOfBoundsException?_Java - Fatal编程技术网

Java 尝试计算文件输入时是否存在StringIndexOutOfBoundsException?

Java 尝试计算文件输入时是否存在StringIndexOutOfBoundsException?,java,Java,对于读取(line.charAt(0)='I')的行,我得到一个StringIndexOutOfBoundsException,其中表示字符串索引在0处超出范围 我不知道该怎么办 QueueLinkedList<Customer> eventqueue = new QueueLinkedList<Customer>(); QueueLinkedList<Customer> customerqueue = new QueueLinkedList<Custo

对于读取
(line.charAt(0)='I')
的行,我得到一个
StringIndexOutOfBoundsException
,其中表示
字符串索引在0处超出范围

我不知道该怎么办

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();
} 
QueueLinkedList事件队列=新建QueueLinkedList();
QueueLinkedList customerqueue=新建QueueLinkedList();
int numberserved=0,totalidletime=0,longestbreak=0,idletime=0,linelength=0,longestline=0;
试一试{
File File=新文件(“customersfile.txt”);
BufferedReader reader=新的BufferedReader(新文件读取器(文件));
弦线;
而((line=reader.readLine())!=null){
//空行
而((line=reader.readLine())!=“”){
//第一行文件
而((line=reader.readLine())!=“300”){
客户newcust=新客户();
如果(行字符(0)='I'){
newcust.id=line.charAt(11);
}
否则{
newcust.arrtime=行子字符串(14);
newcust.setarrtimeecs(newcust.arrtime);
新客户设置离开时间(新客户到达时间);
}
eventqueue.enqueue(newcust);
}
} 
}
}捕获(IOE异常){
e、 printStackTrace();
} 

此行为空,索引0处没有字符,则此字符串为空。您正在执行这一行:

line.charAt(0) == 'I'
这样试试看

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

在第二个while循环中,在与
进行比较之前,您需要
修剪

也可以和equals比较,因为
=运算符比较引用是否指向同一对象。

为什么使用reader.readLine())!=null为什么不只是hasNextLine(),我认为您的方式将保留真正的空行
while ((line = reader.readLine().trim()) != "") {