有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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作为时间。我以后必须输入它们来制作对象

谢谢!


共 (2) 个答案

  1. # 1 楼答案

    在这个循环中:

    for(int i = 0 ; i < typeOfTime.size(); i ++){
        if(String.valueOf(typeOfEvents.get(i))=="WaterOff")
    

    您将迭代到typeOfTime的末尾, 但是访问不同列表的元素,typeOfEvents。 当typeOfTime的大小大于typeOfEvents的大小时,可能会发生IndexOutOfBoundsException, 所以i的值将超出typeOfEvents的界限。 你需要重新设计逻辑来避免这种情况

    您尚未共享typeOfTimetypeOfEvents的声明。 试试这样:

    List<String> typeOfEvents = new ArrayList<String>();
    List<Long> typeOfTime = new ArrayList<Long>();
    

    要在typeOfTime中添加Long,请这样写:

    typeOfTime.add(Long.parseLong(timeSeeker.group()));
    

    代码质量问题

    请注意,您不需要这样的两个循环:

    for(int i =0 ; i < fileToArray.size(); i++) {
        // logic to fill typeOfEvents
    }
    for(int i =0 ; i < fileToArray.size(); i++) {
        // logic to fill typeOfTime
    }
    

    您可以在一个循环中执行这两个操作:

    for(int i =0 ; i < fileToArray.size(); i++) {
        // logic to fill typeOfEvents
        // logic to fill typeOfTime
    }
    

    你的格式很糟糕。使用IDE的函数重新格式化代码,例如在Eclipse中,Control Shift f键盘快捷键可以为您实现这一点。 应用上述建议并修复格式后,代码将如下所示,且更具可读性:

        for(int i =0 ; i < fileToArray.size(); i++){
            String frisk = fileToArray.get(i).toString();
    
            Matcher eventSeeker = eventPat.matcher(frisk);
            while(eventSeeker.find()){
                typeOfEvents.add(eventSeeker.group());
            }
    
            Matcher timeSeeker = timePat.matcher(frisk);
            while(timeSeeker.find()){
                typeOfTime.add(timeSeeker.group());
            }
        }
    
  2. # 2 楼答案

    从我所看到的,索引越界异常可能是因为您有两种模式,一种用于事件,另一种用于提取时间值。
    这是两种不同的模式,仅仅因为事件模式不匹配,并不意味着时间模式不匹配

    例:考虑线

    Event_wrong=Hello,time=1000

    这里第一种模式不匹配,但是第二种模式匹配。 因此,不能保证数组大小相同,因此在迭代时索引超出了界限

    建议:

    • 编写一个正则表达式(或对基于的子字符串进行索引)
    • 如果是正则表达式,请尝试这样的操作“<;=Event=(.+?)\;时间=(\d+{1,5})”
    • 在匹配正则表达式后,尝试调试或打印列表中的值

    其他建议:

    • 从文件中读取行时,检查模式(无需将完整文件保存在内存中)