【问题标题】:Is there any way to count integer elements in text file?有没有办法计算文本文件中的整数元素?
【发布时间】:2019-01-03 17:01:56
【问题描述】:

所以我有一个文本文件:

我想计算第一行的整数个数。

// e.g. The first row : 3 12 1 8 5 8 1 2 1 4 --> 10

我可以使用streamfor 语句或其他方式来做到这一点吗? 我尝试了 for 但它对我不起作用,我找不到任何有用的解决方案。请帮我。

public class Egyszamjatek {

    public static void main(String[] args) throws IOException {
        List<String> game = Files.readAllLines(Paths.get("egyszamjatek.txt"));
        ArrayList<OneGame> games = new ArrayList<>();

        for (String game1 : game) {
            String[] split = game1.split(" ");
            int rounds = Integer.parseInt(split[0]) + Integer.parseInt(split[1]) + Integer.parseInt(split[2])
                    + Integer.parseInt(split[3]) + Integer.parseInt(split[4]) + Integer.parseInt(split[5])
                    + Integer.parseInt(split[6]) + Integer.parseInt(split[7]) + Integer.parseInt(split[8])
                    + Integer.parseInt(split[9]);
            String names = split[10]; 

            games.add(new OneGame(rounds, names));
        }

        System.out.println("3.feladat: number of players : " + game.stream().count());
        System.out.println("4. feladat: number of rounds: "  );

    }

    static class OneGame {
        int rounds;
        String names;

        public OneGame(int rounds, String names) {
            this.rounds = rounds;
            this.names = names;
        }
    }
}

【问题讨论】:

  • 您的解决方案面临的问题是什么?任何异常或没有数据?
  • 问题是我不知道如何计算第一行的整数元素。
  • 名称前的数字是四舍五入。所以我想数回合数,所以我需要数数。

标签: java list arraylist java-stream


【解决方案1】:

for 循环的解决方案

    String firstLine = "3 12 1 8 5 8 1 2 1 4";

    String[] splits = firstLine.split(" ");

    int count = 0 ;
    for(String intStr:splits){
        try {
            int i = Integer.parseInt(intStr);
            count++;
        }catch (NumberFormatException e){
            e.printStackTrace();
        }

    }
    System.out.println(count);

【讨论】:

    【解决方案2】:

    你可以这样做:

    List<OneGame> games = Files.lines(Paths.get("egyszamjatek.txt")) // Stream<String> each line as a single String
            .map(g -> {
                String[] split = g.split(" ");
                int rounds = (int) Arrays.stream(split)
                        .filter(a -> isInteger(a)) // filter only integers
                        .count(); // count how many integers e.g. 10 in your first line
                return new OneGame(rounds, split[rounds]); // create an instance with count and name
            }).collect(Collectors.toList()); // collect to list
    

    其中isInteger(a) 是您可以从this answer 使用的实用程序。它的实现是:

    public static boolean isInteger(String str) {
        if (str == null) {
            return false;
        }
        if (str.isEmpty()) {
            return false;
        }
        int i = 0;
        if (str.charAt(0) == '-') {
            if (str.length() == 1) {
                return false;
            }
            i = 1;
        }
        for (; i < str.length(); i++) {
            char c = str.charAt(i);
            if (c < '0' || c > '9') {
                return false;
            }
        }
        return true;
    }
    

    注意:代码依赖于某些假设,例如回合数的整数值将取代游戏名称,因此使用split[rounds] 访问名称。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-10
      • 1970-01-01
      • 2019-09-03
      • 1970-01-01
      • 1970-01-01
      • 2011-12-19
      • 1970-01-01
      相关资源
      最近更新 更多