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()) != "") {