cant count works ad character with pdfbox in java
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
|
show 9 more comments
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
2
ChangeString sourceCode
toString sourceCode="";
– soorapadman
Nov 23 '18 at 5:03
The variablesourceCode
has to be set to something before you can usesourceCode.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
replacestring text
withsourcecode
– preciousbetine
Nov 23 '18 at 5:09
|
show 9 more comments
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
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
java pdfbox
edited Nov 23 '18 at 5:06
soorapadman
2,62752431
2,62752431
asked Nov 23 '18 at 4:59
Choy Choy
1099
1099
2
ChangeString sourceCode
toString sourceCode="";
– soorapadman
Nov 23 '18 at 5:03
The variablesourceCode
has to be set to something before you can usesourceCode.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
replacestring text
withsourcecode
– preciousbetine
Nov 23 '18 at 5:09
|
show 9 more comments
2
ChangeString sourceCode
toString sourceCode="";
– soorapadman
Nov 23 '18 at 5:03
The variablesourceCode
has to be set to something before you can usesourceCode.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
replacestring text
withsourcecode
– 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
|
show 9 more comments
1 Answer
1
active
oldest
votes
Replace this line sourceCode = sourceCode.replace ("-", "").replace (".", "");
with sourceCode = text.replace ("-", "").replace (".", "");
.and remove the while loop
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%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
Replace this line sourceCode = sourceCode.replace ("-", "").replace (".", "");
with sourceCode = text.replace ("-", "").replace (".", "");
.and remove the while loop
add a comment |
Replace this line sourceCode = sourceCode.replace ("-", "").replace (".", "");
with sourceCode = text.replace ("-", "").replace (".", "");
.and remove the while loop
add a comment |
Replace this line sourceCode = sourceCode.replace ("-", "").replace (".", "");
with sourceCode = text.replace ("-", "").replace (".", "");
.and remove the while loop
Replace this line sourceCode = sourceCode.replace ("-", "").replace (".", "");
with sourceCode = text.replace ("-", "").replace (".", "");
.and remove the while loop
answered Nov 23 '18 at 5:56
preciousbetinepreciousbetine
1,5772418
1,5772418
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%2f53440866%2fcant-count-works-ad-character-with-pdfbox-in-java%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
2
Change
String sourceCode
toString sourceCode="";
– soorapadman
Nov 23 '18 at 5:03
The variable
sourceCode
has to be set to something before you can usesourceCode.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
withsourcecode
– preciousbetine
Nov 23 '18 at 5:09