Is there a way to count the number of times a word has repeated in a string?












0















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"










share|improve this question

























  • 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
















0















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"










share|improve this question

























  • 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














0












0








0


1






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"










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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



















  • 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












2 Answers
2






active

oldest

votes


















1














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





share|improve this answer
























  • 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



















0














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.






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%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









    1














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





    share|improve this answer
























    • 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
















    1














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





    share|improve this answer
























    • 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














    1












    1








    1







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





    share|improve this answer













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






    share|improve this answer












    share|improve this answer



    share|improve this answer










    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



















    • 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













    0














    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.






    share|improve this answer




























      0














      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.






      share|improve this answer


























        0












        0








        0







        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 26 '18 at 5:36









        Saptarshi BasuSaptarshi Basu

        2,04421627




        2,04421627






























            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%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





















































            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

            Wiesbaden

            Marschland

            Dieringhausen