Is there any way to count integer elements in text file?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















So I have a text file as :



enter image description here



and I want to count the number of integers in the first row.



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


Can I do that with a stream or for statement or another way?
I tried with for and it didn't work for me and I couldn't find any useful solution. Please, help me.



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;
}
}
}









share|improve this question

























  • What is the issue that you are facing with your solution? Any Exception or no data?

    – Ankur
    Jan 3 at 17:05











  • The problem is I don't know how to count the integer elements in a first row.

    – James1999
    Jan 3 at 17:08











  • A numbers before the names are rounds. So I want to count the number of rounds, that's why I need to counting the numbers.

    – James1999
    Jan 3 at 17:29


















0















So I have a text file as :



enter image description here



and I want to count the number of integers in the first row.



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


Can I do that with a stream or for statement or another way?
I tried with for and it didn't work for me and I couldn't find any useful solution. Please, help me.



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;
}
}
}









share|improve this question

























  • What is the issue that you are facing with your solution? Any Exception or no data?

    – Ankur
    Jan 3 at 17:05











  • The problem is I don't know how to count the integer elements in a first row.

    – James1999
    Jan 3 at 17:08











  • A numbers before the names are rounds. So I want to count the number of rounds, that's why I need to counting the numbers.

    – James1999
    Jan 3 at 17:29














0












0








0








So I have a text file as :



enter image description here



and I want to count the number of integers in the first row.



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


Can I do that with a stream or for statement or another way?
I tried with for and it didn't work for me and I couldn't find any useful solution. Please, help me.



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;
}
}
}









share|improve this question
















So I have a text file as :



enter image description here



and I want to count the number of integers in the first row.



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


Can I do that with a stream or for statement or another way?
I tried with for and it didn't work for me and I couldn't find any useful solution. Please, help me.



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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 17:34









Naman

46k12102205




46k12102205










asked Jan 3 at 17:01









James1999James1999

93




93













  • What is the issue that you are facing with your solution? Any Exception or no data?

    – Ankur
    Jan 3 at 17:05











  • The problem is I don't know how to count the integer elements in a first row.

    – James1999
    Jan 3 at 17:08











  • A numbers before the names are rounds. So I want to count the number of rounds, that's why I need to counting the numbers.

    – James1999
    Jan 3 at 17:29



















  • What is the issue that you are facing with your solution? Any Exception or no data?

    – Ankur
    Jan 3 at 17:05











  • The problem is I don't know how to count the integer elements in a first row.

    – James1999
    Jan 3 at 17:08











  • A numbers before the names are rounds. So I want to count the number of rounds, that's why I need to counting the numbers.

    – James1999
    Jan 3 at 17:29

















What is the issue that you are facing with your solution? Any Exception or no data?

– Ankur
Jan 3 at 17:05





What is the issue that you are facing with your solution? Any Exception or no data?

– Ankur
Jan 3 at 17:05













The problem is I don't know how to count the integer elements in a first row.

– James1999
Jan 3 at 17:08





The problem is I don't know how to count the integer elements in a first row.

– James1999
Jan 3 at 17:08













A numbers before the names are rounds. So I want to count the number of rounds, that's why I need to counting the numbers.

– James1999
Jan 3 at 17:29





A numbers before the names are rounds. So I want to count the number of rounds, that's why I need to counting the numbers.

– James1999
Jan 3 at 17:29












2 Answers
2






active

oldest

votes


















0














solution with for loop



    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);





share|improve this answer































    0














    You could do something like:



    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


    where isInteger(a) is a util that you can use from this answer. Its implementation would be :



    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;
    }


    Note: the code relies on certain assumptions for e.g. the integer values for the number of rounds would supersede the name of the game and hence uses split[rounds] to access the name.






    share|improve this answer


























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54026666%2fis-there-any-way-to-count-integer-elements-in-text-file%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      solution with for loop



          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);





      share|improve this answer




























        0














        solution with for loop



            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);





        share|improve this answer


























          0












          0








          0







          solution with for loop



              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);





          share|improve this answer













          solution with for loop



              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);






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 3 at 17:27









          SurajSuraj

          110110




          110110

























              0














              You could do something like:



              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


              where isInteger(a) is a util that you can use from this answer. Its implementation would be :



              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;
              }


              Note: the code relies on certain assumptions for e.g. the integer values for the number of rounds would supersede the name of the game and hence uses split[rounds] to access the name.






              share|improve this answer






























                0














                You could do something like:



                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


                where isInteger(a) is a util that you can use from this answer. Its implementation would be :



                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;
                }


                Note: the code relies on certain assumptions for e.g. the integer values for the number of rounds would supersede the name of the game and hence uses split[rounds] to access the name.






                share|improve this answer




























                  0












                  0








                  0







                  You could do something like:



                  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


                  where isInteger(a) is a util that you can use from this answer. Its implementation would be :



                  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;
                  }


                  Note: the code relies on certain assumptions for e.g. the integer values for the number of rounds would supersede the name of the game and hence uses split[rounds] to access the name.






                  share|improve this answer















                  You could do something like:



                  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


                  where isInteger(a) is a util that you can use from this answer. Its implementation would be :



                  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;
                  }


                  Note: the code relies on certain assumptions for e.g. the integer values for the number of rounds would supersede the name of the game and hence uses split[rounds] to access the name.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jan 3 at 17:39

























                  answered Jan 3 at 17:14









                  NamanNaman

                  46k12102205




                  46k12102205






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54026666%2fis-there-any-way-to-count-integer-elements-in-text-file%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      MongoDB - Not Authorized To Execute Command

                      How to fix TextFormField cause rebuild widget in Flutter

                      in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith