仅当中间数字在 java 中递增超过 1 时才打印 =====

print ===== only if middle number is incremented by more than 1 in java

我的输入文件格式如下:

1,1,1
2,1,0
3,1,0
4,1,0
5,1,0
6,1,0
7,1,0
8,1,0
1,3,0
2,3,0
3,3,0
4,3,0
5,3,0
6,3,1
7,3,1
8,3,0
1,4,0
2,4,1
3,4,0
4,4,0
5,4,0
6,4,0
7,4,0
8,4,1
1,5,1
2,5,0
3,5,0
4,5,0
5,5,0
6,5,0
7,5,1
8,5,1

我正在读取这个文件并将其存储到一个字符串列表中,如下所示,然后我用逗号分隔每一行。中间的数字在 8 行后递增,我想打印 =============== 仅当它递增超过 1 时。我当前的输出如下:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ReadFileLineByLineUsingBufferedReader {

    public static void main(String[] args) {
        BufferedReader reader;
        List<String> mylist= new ArrayList<String>(); 

        try {
            reader = new BufferedReader(new FileReader(
                    "C:\Users\mouna\ownCloud\Mouna Hammoudi\dumps\Python\dataMachineLearning.txt"));
            String line = reader.readLine();
            while (line != null) {
                // read next line
                mylist.add(line); 

                line = reader.readLine();
            }   
            int counter=0; 
            int last=-1; 
            for(String myline: mylist) {
                    String[] splitted = myline.split("\,"); 
                    System.out.println(splitted[0]+"  "+splitted[1]+"   "+splitted[2]);
                    int num=Integer.parseInt(splitted[1])+1; 
                    counter++;
                    if(counter%8==0 && num!=last-1) {
                        System.out.println("=============================================");
                    }
                    last=num; 
                    if(counter==8) {
                        counter=0; 
                    }
                     
                
                }
                
                
                
                
            
            
            
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

这是我的输出:

 1  1   1
    2  1   0
    3  1   0
    4  1   0
    5  1   0
    6  1   0
    7  1   0
    8  1   0
    =============================================
    1  3   0
    2  3   0
    3  3   0
    4  3   0
    5  3   0
    6  3   1
    7  3   1
    8  3   0
    =============================================
    1  4   0
    2  4   1
    3  4   0
    4  4   0
    5  4   0
    6  4   0
    7  4   0
    8  4   1
    =============================================
    1  5   1
    2  5   0
    3  5   0
    4  5   0
    5  5   0
    6  5   0
    7  5   1
    8  5   1

这是不正确的,因为我只想打印 ====================== 如果中间数字递增超过 1。正确的输出应如下所示:

1  1   1
2  1   0
3  1   0
4  1   0
5  1   0
6  1   0
7  1   0
8  1   0
=============================================
1  3   0
2  3   0
3  3   0
4  3   0
5  3   0
6  3   1
7  3   1
8  3   0
1  4   0
2  4   1
3  4   0
4  4   0
5  4   0
6  4   0
7  4   0
8  4   1
1  5   1
2  5   0
3  5   0
4  5   0
5  5   0
6  5   0
7  5   1
8  5   1

我该如何解决这个问题?

我认为您走在正确的道路上,只是在 if 语句中进行了错误的比较。我还对您的代码进行了一些调整以稍微简化它。

public static void main(String[] args) {
    try {
        List<String> list = Files.readAllLines(Paths.get("C:\Users\mouna\ownCloud\Mouna Hammoudi\dumps\Python\dataMachineLearning.txt"));
        int last = 0;
        for(String myLine : list) {
            String[] array = myLine.split(",");
            int counter = Integer.parseInt(array[0]);
            int num = Integer.parseInt(array[1]);
            if(counter == 1 && num > last+1) {
                System.out.println("======");
            }
            System.out.println(array[0]+"  "+array[1]+"  "+array[2]);
            last = num;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

我保留了您的代码,只是将检查替换为检查中间数字的差异。我已将您的旧支票留作注释行,如果需要,可以将其添加到支票和条件中。从你的问题中,我只知道只有当差异大于 1 时才应该打印虚线,所以我拿出了你的支票。这是修改后的代码。

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ReadFileLineByLineUsingBufferedReader {

    public static void main(String[] args) {
        BufferedReader reader;
        List<String> mylist= new ArrayList<String>(); 
        int prevNo = 0;

        try {
            reader = new BufferedReader(new FileReader(
                    "C:\Users\Admin.MSI\eclipse-workspace\Mouna\dataMachineLearning.txt"));
            String line = reader.readLine();
            while (line != null) {
                // read next line
                mylist.add(line); 

                line = reader.readLine();
            }   
            int counter=0; 
            int last=-1; 
            for(String myline: mylist) {
                    String[] splitted = myline.split("\,"); 
                    
                    int num=Integer.parseInt(splitted[1])+1; 
                    counter++;
                    //if(counter%8==0 && num!=last-1 && Math.abs(Integer.parseInt(splitted[1]) - tempa) > 1 ) {
                    if(Math.abs(Integer.parseInt(splitted[1]) - prevNo) > 1 ) {
                        
                        System.out.println("=============================================");
                    }
                    
                    System.out.println(splitted[0]+"  "+splitted[1]+"   "+splitted[2]);
                    last=num; 
                    if(counter==8) {
                        counter=0; 
                    }
                    prevNo = Integer.parseInt(splitted[1]);
                }                
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

您需要将前面的中间数字存储在 Integer 中,因为原始 int 不支持 null 值。我们使用 null 来检查我们是否正在评估第一行。

比较middle-1>prev ? LINE +"\n" + currentLine : currentLine检查中间数减1是否大于前一个中间数。如果是,我们打印等号行(常量LINE)加上当前行,否则我们简单地打印当前行。

在你之前的代码中,你循环了两次,这大大降低了效率。一个循环是可能的,并且可能是最紧凑和最有效的解决方案,如下所示:

private static final String LINE =  "=============================================";
public static void process(File file) {
    try(BufferedReader br = new BufferedReader(new FileReader(file))){
        String currentLine;
        Integer prev = null;
        int linesRead = 0;
        while((currentLine = br.readLine()) != null) {
            int middle = Integer.parseInt(currentLine.split(",")[1]);
            linesRead++;
            if(prev==null) {
                System.out.println(currentLine);
            }else {
                System.out.println(middle-1>prev && linesReader%8==0 ? LINE +"\n" + currentLine : currentLine);
            }
            prev=middle;
        }
    }catch(Exception e) {
        e.printStackTrace();
    }
}

输出:

1,1,1
2,1,0
3,1,0
4,1,0
5,1,0
6,1,0
7,1,0
8,1,0
=============================================
1,3,0
2,3,0
3,3,0
4,3,0
5,3,0
6,3,1
7,3,1
8,3,0
1,4,0
2,4,1
3,4,0
4,4,0
5,4,0
6,4,0
7,4,0
8,4,1
1,5,1
2,5,0
3,5,0
4,5,0
5,5,0
6,5,0
7,5,1
8,5,1

逻辑是,获取当前行splitted[1]值和下一行splitted[1]值。如果差异是 greater 那么 1 那么 print("===========").

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
    
public class ReadFileLineByLineUsingBufferedReader {
    
    public static void main(String[] args) {
        BufferedReader reader;
        List<String> mylist= new ArrayList<String>(); 
        try {
            reader = new BufferedReader(new FileReader("C:\Users\mouna\ownCloud\Mouna Hammoudi\dumps\Python\dataMachineLearning.txt"));
            String line = reader.readLine();
            while (line != null) {
                mylist.add(line); 
                line = reader.readLine();
            }    
            int last = -1; 
            for(int i = 0; i < mylist.size() - 1; i++) {
                    String[] split_current = mylist.get(i).split("\,"); 
                    String[] split_next = mylist.get(i + 1).split("\,"); 
                    System.out.println(split_current[0]+"  "+split_current[1]+"   "+split_current[2]);
                    int num_current = Integer.parseInt(split_current[1]);
                    int num_next = Integer.parseInt(split_next[1]);
                    if(num_next - num_current > 1){
                        System.out.println("=============================================");
                    }
                }    
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}