【问题标题】:print ===== only if middle number is incremented by more than 1 in java打印 ===== 仅当中间数字在 java 中增加超过 1 时
【发布时间】:2021-05-07 02:53:49
【问题描述】:

我的输入文件的格式如下:

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 行后递增,我想打印 =============== 仅当它增加超过一时。我目前的输出如下:

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

我该如何解决这个问题?

【问题讨论】:

  • 将当前迭代的中间数存储在单独的变量中。在下一次迭代中,将其与此值进行比较以检查是否大于 1 个案例。
  • 请注意,您可以将整个“读取文件”循环替换为 List&lt;String&gt; lines = Files.readAllLines(Path.of("myFile.txt"));

标签: java arrays list increment


【解决方案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();
        }
    }
}

【讨论】:

  • .split("\\,") - 两个反斜杠不是必需的。
  • 是的,它不是必需的,因为 "," 不像 regex 中的 "." 那样是 special character。谢谢:)
【解决方案2】:

我认为您走在正确的道路上,只是在您的 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();
    }
}

【讨论】:

    【解决方案3】:

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

    比较middle-1&gt;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
    

    【讨论】:

      【解决方案4】:

      我保留了您的代码,只是将检查替换为检查中间数字的差异。我已将您的旧支票作为注释行留下,如果需要,可以将其添加到支票和条件中。从你的问题中,我只知道只有当差值大于 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();
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-03-08
        • 1970-01-01
        • 1970-01-01
        • 2011-12-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多