cant count works ad character with pdfbox in java












-2















class ReadPDF {


public void Read() throws IOException {

int amountOfWords = 0;
int amountOfChars = 0;
String sourceCode ="";

try {
PDDocument doc = PDDocument.load(new File("C:\Users\ccw\Desktop\articles\RECYCLING-BEHAVIOUR-AMONG-MALAYSIAN-TERTIARY-STUDENTS.pdf"));
String text = new PDFTextStripper().getText(doc);

sourceCode = sourceCode.replace ("-", "").replace (".", "");

while(doc!=null){
String words = sourceCode.split(" ");
amountOfWords = amountOfWords + words.length;
for (String word : words) {
amountOfChars = amountOfChars + word.length();
}
}

System.out.println("Amount of Chars is " + amountOfChars);
System.out.println("Amount of Words is " + (amountOfWords + 1));
System.out.println("Average Word Length is "+ (amountOfChars/amountOfWords));


}catch (IOException e) {
System.out.println(e);
}

}

}


I'm trying to count all the words and character in a pdf file by using pdfbox.
But now i getting an error, sourceCode is not initialize










share|improve this question




















  • 2





    Change String sourceCode to String sourceCode="";

    – soorapadman
    Nov 23 '18 at 5:03













  • The variable sourceCode has to be set to something before you can use sourceCode.replace() otherwise it will remain null. Please edit your answer to include the code in text format instead of a picture. Thanks.

    – Jonathan Van Dam
    Nov 23 '18 at 5:04











  • @JonathanVanDam post edited

    – Choy
    Nov 23 '18 at 5:06






  • 1





    btw is it correct ?in this line: while(doc!=null)

    – Choy
    Nov 23 '18 at 5:09






  • 1





    replace string text with sourcecode

    – preciousbetine
    Nov 23 '18 at 5:09
















-2















class ReadPDF {


public void Read() throws IOException {

int amountOfWords = 0;
int amountOfChars = 0;
String sourceCode ="";

try {
PDDocument doc = PDDocument.load(new File("C:\Users\ccw\Desktop\articles\RECYCLING-BEHAVIOUR-AMONG-MALAYSIAN-TERTIARY-STUDENTS.pdf"));
String text = new PDFTextStripper().getText(doc);

sourceCode = sourceCode.replace ("-", "").replace (".", "");

while(doc!=null){
String words = sourceCode.split(" ");
amountOfWords = amountOfWords + words.length;
for (String word : words) {
amountOfChars = amountOfChars + word.length();
}
}

System.out.println("Amount of Chars is " + amountOfChars);
System.out.println("Amount of Words is " + (amountOfWords + 1));
System.out.println("Average Word Length is "+ (amountOfChars/amountOfWords));


}catch (IOException e) {
System.out.println(e);
}

}

}


I'm trying to count all the words and character in a pdf file by using pdfbox.
But now i getting an error, sourceCode is not initialize










share|improve this question




















  • 2





    Change String sourceCode to String sourceCode="";

    – soorapadman
    Nov 23 '18 at 5:03













  • The variable sourceCode has to be set to something before you can use sourceCode.replace() otherwise it will remain null. Please edit your answer to include the code in text format instead of a picture. Thanks.

    – Jonathan Van Dam
    Nov 23 '18 at 5:04











  • @JonathanVanDam post edited

    – Choy
    Nov 23 '18 at 5:06






  • 1





    btw is it correct ?in this line: while(doc!=null)

    – Choy
    Nov 23 '18 at 5:09






  • 1





    replace string text with sourcecode

    – preciousbetine
    Nov 23 '18 at 5:09














-2












-2








-2








class ReadPDF {


public void Read() throws IOException {

int amountOfWords = 0;
int amountOfChars = 0;
String sourceCode ="";

try {
PDDocument doc = PDDocument.load(new File("C:\Users\ccw\Desktop\articles\RECYCLING-BEHAVIOUR-AMONG-MALAYSIAN-TERTIARY-STUDENTS.pdf"));
String text = new PDFTextStripper().getText(doc);

sourceCode = sourceCode.replace ("-", "").replace (".", "");

while(doc!=null){
String words = sourceCode.split(" ");
amountOfWords = amountOfWords + words.length;
for (String word : words) {
amountOfChars = amountOfChars + word.length();
}
}

System.out.println("Amount of Chars is " + amountOfChars);
System.out.println("Amount of Words is " + (amountOfWords + 1));
System.out.println("Average Word Length is "+ (amountOfChars/amountOfWords));


}catch (IOException e) {
System.out.println(e);
}

}

}


I'm trying to count all the words and character in a pdf file by using pdfbox.
But now i getting an error, sourceCode is not initialize










share|improve this question
















class ReadPDF {


public void Read() throws IOException {

int amountOfWords = 0;
int amountOfChars = 0;
String sourceCode ="";

try {
PDDocument doc = PDDocument.load(new File("C:\Users\ccw\Desktop\articles\RECYCLING-BEHAVIOUR-AMONG-MALAYSIAN-TERTIARY-STUDENTS.pdf"));
String text = new PDFTextStripper().getText(doc);

sourceCode = sourceCode.replace ("-", "").replace (".", "");

while(doc!=null){
String words = sourceCode.split(" ");
amountOfWords = amountOfWords + words.length;
for (String word : words) {
amountOfChars = amountOfChars + word.length();
}
}

System.out.println("Amount of Chars is " + amountOfChars);
System.out.println("Amount of Words is " + (amountOfWords + 1));
System.out.println("Average Word Length is "+ (amountOfChars/amountOfWords));


}catch (IOException e) {
System.out.println(e);
}

}

}


I'm trying to count all the words and character in a pdf file by using pdfbox.
But now i getting an error, sourceCode is not initialize







java pdfbox






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 5:06









soorapadman

2,62752431




2,62752431










asked Nov 23 '18 at 4:59









Choy Choy

1099




1099








  • 2





    Change String sourceCode to String sourceCode="";

    – soorapadman
    Nov 23 '18 at 5:03













  • The variable sourceCode has to be set to something before you can use sourceCode.replace() otherwise it will remain null. Please edit your answer to include the code in text format instead of a picture. Thanks.

    – Jonathan Van Dam
    Nov 23 '18 at 5:04











  • @JonathanVanDam post edited

    – Choy
    Nov 23 '18 at 5:06






  • 1





    btw is it correct ?in this line: while(doc!=null)

    – Choy
    Nov 23 '18 at 5:09






  • 1





    replace string text with sourcecode

    – preciousbetine
    Nov 23 '18 at 5:09














  • 2





    Change String sourceCode to String sourceCode="";

    – soorapadman
    Nov 23 '18 at 5:03













  • The variable sourceCode has to be set to something before you can use sourceCode.replace() otherwise it will remain null. Please edit your answer to include the code in text format instead of a picture. Thanks.

    – Jonathan Van Dam
    Nov 23 '18 at 5:04











  • @JonathanVanDam post edited

    – Choy
    Nov 23 '18 at 5:06






  • 1





    btw is it correct ?in this line: while(doc!=null)

    – Choy
    Nov 23 '18 at 5:09






  • 1





    replace string text with sourcecode

    – preciousbetine
    Nov 23 '18 at 5:09








2




2





Change String sourceCode to String sourceCode="";

– soorapadman
Nov 23 '18 at 5:03







Change String sourceCode to String sourceCode="";

– soorapadman
Nov 23 '18 at 5:03















The variable sourceCode has to be set to something before you can use sourceCode.replace() otherwise it will remain null. Please edit your answer to include the code in text format instead of a picture. Thanks.

– Jonathan Van Dam
Nov 23 '18 at 5:04





The variable sourceCode has to be set to something before you can use sourceCode.replace() otherwise it will remain null. Please edit your answer to include the code in text format instead of a picture. Thanks.

– Jonathan Van Dam
Nov 23 '18 at 5:04













@JonathanVanDam post edited

– Choy
Nov 23 '18 at 5:06





@JonathanVanDam post edited

– Choy
Nov 23 '18 at 5:06




1




1





btw is it correct ?in this line: while(doc!=null)

– Choy
Nov 23 '18 at 5:09





btw is it correct ?in this line: while(doc!=null)

– Choy
Nov 23 '18 at 5:09




1




1





replace string text with sourcecode

– preciousbetine
Nov 23 '18 at 5:09





replace string text with sourcecode

– preciousbetine
Nov 23 '18 at 5:09












1 Answer
1






active

oldest

votes


















1














Replace this line sourceCode = sourceCode.replace ("-", "").replace (".", ""); with sourceCode = text.replace ("-", "").replace (".", ""); .and remove the while loop






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%2f53440866%2fcant-count-works-ad-character-with-pdfbox-in-java%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Replace this line sourceCode = sourceCode.replace ("-", "").replace (".", ""); with sourceCode = text.replace ("-", "").replace (".", ""); .and remove the while loop






    share|improve this answer




























      1














      Replace this line sourceCode = sourceCode.replace ("-", "").replace (".", ""); with sourceCode = text.replace ("-", "").replace (".", ""); .and remove the while loop






      share|improve this answer


























        1












        1








        1







        Replace this line sourceCode = sourceCode.replace ("-", "").replace (".", ""); with sourceCode = text.replace ("-", "").replace (".", ""); .and remove the while loop






        share|improve this answer













        Replace this line sourceCode = sourceCode.replace ("-", "").replace (".", ""); with sourceCode = text.replace ("-", "").replace (".", ""); .and remove the while loop







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 5:56









        preciousbetinepreciousbetine

        1,5772418




        1,5772418
































            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%2f53440866%2fcant-count-works-ad-character-with-pdfbox-in-java%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

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            RAC Tourist Trophy