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;
}
So I have a text file as :
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
add a comment |
So I have a text file as :
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
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
add a comment |
So I have a text file as :
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
So I have a text file as :
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
java list arraylist java-stream
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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);
add a comment |
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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);
add a comment |
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);
add a comment |
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);
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);
answered Jan 3 at 17:27


SurajSuraj
110110
110110
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Jan 3 at 17:39
answered Jan 3 at 17:14


NamanNaman
46k12102205
46k12102205
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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