java文件输入到arrayList。与用于创建新对象的arrayList相同
我正在开发一个程序,在这个程序中,我从一个文件中获取输入,并使用正则表达式输出我需要的内容。我踢出整数和字符串。每行一个。每一行都有一个对象的信息
try{
Scanner getAll = new Scanner(new FileReader(eventsFile));
while(getAll.hasNextLine()){
fileToArray.add(getAll.nextLine());
}
}
catch(FileNotFoundException e){
System.out.println("the file name you entered is not present in the directory");
}
catch(IOException e){
System.out.println("An I/O error present");
}
System.out.println(fileToArray.size());
// we put our regular expression here which kicks out the event
//eventPat gets everything after Event= and before ,time
Pattern eventPat = Pattern.compile("(?<=Event=)(.*)(?=,time)");
//timePat looks for everynumber from 0-9 whose size is 1 up to 5
Pattern timePat = Pattern.compile("[0-9]{1,5}");
然后我继续将int添加到一个数组中,将字符串添加到另一个数组中
// this for loop takes the fileToArray which has all the example file contents inside and matches it for eventPat and adds its findings to an array called typeOfEvents
for(int i =0 ; i < fileToArray.size(); i++){
String friskOne = fileToArray.get(i).toString();
Matcher eventSeeker = eventPat.matcher(friskOne);
while(eventSeeker.find()){
typeOfEvents.add(eventSeeker.group());
}
}
// this for loop takes the fileToArray which has all the example file contents inside and matches it for timePat and adds its findings to an array called typeOfTime
for(int i =0 ; i < fileToArray.size(); i++){
String frisk = fileToArray.get(i).toString();
Matcher timeSeeker = timePat.matcher(frisk);
while(timeSeeker.find()){
typeOfTime.add(timeSeeker.group());
}
}
然后我使用了一个循环,我在其中制作对象。我获取每个数组的第一个索引并将它们配对。有些对象可能不需要,因为最初没有在文件中创建它们的说明
// here the loop takes the first event in the typeOfEvent array and runs it with the first
//this works since everything in , typeOfEvents and typeOfTime is added in the order that it is //present in the example file. The cast is added
//so that the data is changed from object to type long and string
public void action() {
for(int i = 0 ; i < typeOfTime.size(); i ++){
if(String.valueOf(typeOfEvents.get(i))=="WaterOff")
{Long a = (Long.valueOf((String)typeOfTime.get(i)));
addEvent(new WaterOff(a));}// Clarification needed does this convert to long from
else if ((String)typeOfEvents.get(i)=="WindowMalfunction")
addEvent(new WindowMalfunction(Long.valueOf((String)typeOfTime.get(i)))); }
错误一是我得到的索引超出了界限。 其次,我认为我没有将文件输入转换为正确的类型。我需要将字符串数组中的所有字符串转换成字符串,并将存储在int数组中的所有int转换成long,这就是生成对象所需的。 实例 该文件有一行——Event=WaterOff,time=10000 我将waterOff存储在EventSeek中,因为waterOff是一个事件。我在TimeSeek中保留了10000作为时间。我以后必须输入它们来制作对象
谢谢!
# 1 楼答案
在这个循环中:
您将迭代到
typeOfTime
的末尾, 但是访问不同列表的元素,typeOfEvents
。 当typeOfTime
的大小大于typeOfEvents
的大小时,可能会发生IndexOutOfBoundsException, 所以i
的值将超出typeOfEvents
的界限。 你需要重新设计逻辑来避免这种情况您尚未共享
typeOfTime
和typeOfEvents
的声明。 试试这样:要在
typeOfTime
中添加Long
,请这样写:代码质量问题
请注意,您不需要这样的两个循环:
您可以在一个循环中执行这两个操作:
你的格式很糟糕。使用IDE的函数重新格式化代码,例如在Eclipse中,Control Shift f键盘快捷键可以为您实现这一点。 应用上述建议并修复格式后,代码将如下所示,且更具可读性:
# 2 楼答案
从我所看到的,索引越界异常可能是因为您有两种模式,一种用于事件,另一种用于提取时间值。
例:考虑线这是两种不同的模式,仅仅因为事件模式不匹配,并不意味着时间模式不匹配
这里第一种模式不匹配,但是第二种模式匹配。 因此,不能保证数组大小相同,因此在迭代时索引超出了界限
建议:
其他建议: