Is there a way to count the number of times a word has repeated in a string?
My string has over 100000 words. Its a book. It contains about x number of chapters:
chapter 1
text text text
chapter 2
text text text
and so on
How do I get the total number of chapters?(last chapter number)?
for example: chapter 117
I tried this :
String words = book.split(" ");
ArrayList<Integer> chapterPositions = new ArrayList<Integer>();
int count = 0;
for (String a : words) {
if (a.equals("Chapter")) {
chapterPositions.add(count + 1);
}
count++;
}
num_chapters = Integer.parseInt(words[(chapterPositions.get(chapterPositions.size() - 1))]);
Toast.makeText(getApplicationContext(), Integer.toString(num_chapters), Toast.LENGTH_LONG).show();
but get exception :NumberFormatException: For input string: "1 The"
java string
add a comment |
My string has over 100000 words. Its a book. It contains about x number of chapters:
chapter 1
text text text
chapter 2
text text text
and so on
How do I get the total number of chapters?(last chapter number)?
for example: chapter 117
I tried this :
String words = book.split(" ");
ArrayList<Integer> chapterPositions = new ArrayList<Integer>();
int count = 0;
for (String a : words) {
if (a.equals("Chapter")) {
chapterPositions.add(count + 1);
}
count++;
}
num_chapters = Integer.parseInt(words[(chapterPositions.get(chapterPositions.size() - 1))]);
Toast.makeText(getApplicationContext(), Integer.toString(num_chapters), Toast.LENGTH_LONG).show();
but get exception :NumberFormatException: For input string: "1 The"
java string
This could be done easily in Linux but are you using Java here?
– JorgeeFG
Nov 26 '18 at 1:58
yes sir. absolutely
– user9555243
Nov 26 '18 at 2:01
add a comment |
My string has over 100000 words. Its a book. It contains about x number of chapters:
chapter 1
text text text
chapter 2
text text text
and so on
How do I get the total number of chapters?(last chapter number)?
for example: chapter 117
I tried this :
String words = book.split(" ");
ArrayList<Integer> chapterPositions = new ArrayList<Integer>();
int count = 0;
for (String a : words) {
if (a.equals("Chapter")) {
chapterPositions.add(count + 1);
}
count++;
}
num_chapters = Integer.parseInt(words[(chapterPositions.get(chapterPositions.size() - 1))]);
Toast.makeText(getApplicationContext(), Integer.toString(num_chapters), Toast.LENGTH_LONG).show();
but get exception :NumberFormatException: For input string: "1 The"
java string
My string has over 100000 words. Its a book. It contains about x number of chapters:
chapter 1
text text text
chapter 2
text text text
and so on
How do I get the total number of chapters?(last chapter number)?
for example: chapter 117
I tried this :
String words = book.split(" ");
ArrayList<Integer> chapterPositions = new ArrayList<Integer>();
int count = 0;
for (String a : words) {
if (a.equals("Chapter")) {
chapterPositions.add(count + 1);
}
count++;
}
num_chapters = Integer.parseInt(words[(chapterPositions.get(chapterPositions.size() - 1))]);
Toast.makeText(getApplicationContext(), Integer.toString(num_chapters), Toast.LENGTH_LONG).show();
but get exception :NumberFormatException: For input string: "1 The"
java string
java string
edited Nov 26 '18 at 3:28
user9555243
asked Nov 26 '18 at 1:45
user9555243user9555243
408
408
This could be done easily in Linux but are you using Java here?
– JorgeeFG
Nov 26 '18 at 1:58
yes sir. absolutely
– user9555243
Nov 26 '18 at 2:01
add a comment |
This could be done easily in Linux but are you using Java here?
– JorgeeFG
Nov 26 '18 at 1:58
yes sir. absolutely
– user9555243
Nov 26 '18 at 2:01
This could be done easily in Linux but are you using Java here?
– JorgeeFG
Nov 26 '18 at 1:58
This could be done easily in Linux but are you using Java here?
– JorgeeFG
Nov 26 '18 at 1:58
yes sir. absolutely
– user9555243
Nov 26 '18 at 2:01
yes sir. absolutely
– user9555243
Nov 26 '18 at 2:01
add a comment |
2 Answers
2
active
oldest
votes
Because there is not only space, but there are also new line characters, tab, etc.. in your "book" string.
You should use this regular expression instead:
String words = book.split("\s+");
there are special characters as well
– user9555243
Nov 26 '18 at 3:55
man you are the G.O.A.T!!
– user9555243
Nov 26 '18 at 3:58
add a comment |
I think a better way would have been reading the file line by line and using a regular expression like Chapter\s+[0-9]+
with the Pattern
and Matcher
java classes and then count the number of match.
Thus you don't load the entire file in memory, you don't need to first iterate through the string to just split and then iterate again to find a match.
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%2f53473784%2fis-there-a-way-to-count-the-number-of-times-a-word-has-repeated-in-a-string%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
Because there is not only space, but there are also new line characters, tab, etc.. in your "book" string.
You should use this regular expression instead:
String words = book.split("\s+");
there are special characters as well
– user9555243
Nov 26 '18 at 3:55
man you are the G.O.A.T!!
– user9555243
Nov 26 '18 at 3:58
add a comment |
Because there is not only space, but there are also new line characters, tab, etc.. in your "book" string.
You should use this regular expression instead:
String words = book.split("\s+");
there are special characters as well
– user9555243
Nov 26 '18 at 3:55
man you are the G.O.A.T!!
– user9555243
Nov 26 '18 at 3:58
add a comment |
Because there is not only space, but there are also new line characters, tab, etc.. in your "book" string.
You should use this regular expression instead:
String words = book.split("\s+");
Because there is not only space, but there are also new line characters, tab, etc.. in your "book" string.
You should use this regular expression instead:
String words = book.split("\s+");
answered Nov 26 '18 at 3:46
TheIronHeadTheIronHead
413
413
there are special characters as well
– user9555243
Nov 26 '18 at 3:55
man you are the G.O.A.T!!
– user9555243
Nov 26 '18 at 3:58
add a comment |
there are special characters as well
– user9555243
Nov 26 '18 at 3:55
man you are the G.O.A.T!!
– user9555243
Nov 26 '18 at 3:58
there are special characters as well
– user9555243
Nov 26 '18 at 3:55
there are special characters as well
– user9555243
Nov 26 '18 at 3:55
man you are the G.O.A.T!!
– user9555243
Nov 26 '18 at 3:58
man you are the G.O.A.T!!
– user9555243
Nov 26 '18 at 3:58
add a comment |
I think a better way would have been reading the file line by line and using a regular expression like Chapter\s+[0-9]+
with the Pattern
and Matcher
java classes and then count the number of match.
Thus you don't load the entire file in memory, you don't need to first iterate through the string to just split and then iterate again to find a match.
add a comment |
I think a better way would have been reading the file line by line and using a regular expression like Chapter\s+[0-9]+
with the Pattern
and Matcher
java classes and then count the number of match.
Thus you don't load the entire file in memory, you don't need to first iterate through the string to just split and then iterate again to find a match.
add a comment |
I think a better way would have been reading the file line by line and using a regular expression like Chapter\s+[0-9]+
with the Pattern
and Matcher
java classes and then count the number of match.
Thus you don't load the entire file in memory, you don't need to first iterate through the string to just split and then iterate again to find a match.
I think a better way would have been reading the file line by line and using a regular expression like Chapter\s+[0-9]+
with the Pattern
and Matcher
java classes and then count the number of match.
Thus you don't load the entire file in memory, you don't need to first iterate through the string to just split and then iterate again to find a match.
answered Nov 26 '18 at 5:36
Saptarshi BasuSaptarshi Basu
2,04421627
2,04421627
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%2f53473784%2fis-there-a-way-to-count-the-number-of-times-a-word-has-repeated-in-a-string%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
This could be done easily in Linux but are you using Java here?
– JorgeeFG
Nov 26 '18 at 1:58
yes sir. absolutely
– user9555243
Nov 26 '18 at 2:01