how to count the number of alphabet in a pdf file in java
In this program, I need to count the number of all alphabets (a-z) and the code below is only able to count the number of "u". So if I want to count 26 of it, do I need to write 26 if statement in the for loop?
the output should be:
a:389
b:777
c:909
..
..
Please give me other solution
String words = sourceCode.split(" ");
amountOfWords = amountOfWords + words.length;
for (String word : words) {
amountOfChars = amountOfChars + word.length();
for (int i = 0; i < word.length(); i++) {
if (word.charAt(i) == 'u' || word.charAt(i) == 'U') {
u++;
}
}
}
java count pdfbox
add a comment |
In this program, I need to count the number of all alphabets (a-z) and the code below is only able to count the number of "u". So if I want to count 26 of it, do I need to write 26 if statement in the for loop?
the output should be:
a:389
b:777
c:909
..
..
Please give me other solution
String words = sourceCode.split(" ");
amountOfWords = amountOfWords + words.length;
for (String word : words) {
amountOfChars = amountOfChars + word.length();
for (int i = 0; i < word.length(); i++) {
if (word.charAt(i) == 'u' || word.charAt(i) == 'U') {
u++;
}
}
}
java count pdfbox
Please look at the word count questions here on SO, e.g. stackoverflow.com/questions/29122394/… It's basically the same, just do it for the characters, i.e.flatMap
each word to the chars in an additional step.
– AKSW
Nov 23 '18 at 7:22
Possible duplicate of Checking for Alphabets in a String in java
– Arnaud
Nov 23 '18 at 7:26
add a comment |
In this program, I need to count the number of all alphabets (a-z) and the code below is only able to count the number of "u". So if I want to count 26 of it, do I need to write 26 if statement in the for loop?
the output should be:
a:389
b:777
c:909
..
..
Please give me other solution
String words = sourceCode.split(" ");
amountOfWords = amountOfWords + words.length;
for (String word : words) {
amountOfChars = amountOfChars + word.length();
for (int i = 0; i < word.length(); i++) {
if (word.charAt(i) == 'u' || word.charAt(i) == 'U') {
u++;
}
}
}
java count pdfbox
In this program, I need to count the number of all alphabets (a-z) and the code below is only able to count the number of "u". So if I want to count 26 of it, do I need to write 26 if statement in the for loop?
the output should be:
a:389
b:777
c:909
..
..
Please give me other solution
String words = sourceCode.split(" ");
amountOfWords = amountOfWords + words.length;
for (String word : words) {
amountOfChars = amountOfChars + word.length();
for (int i = 0; i < word.length(); i++) {
if (word.charAt(i) == 'u' || word.charAt(i) == 'U') {
u++;
}
}
}
java count pdfbox
java count pdfbox
edited Nov 23 '18 at 12:01
Choy
asked Nov 23 '18 at 7:20
Choy Choy
598
598
Please look at the word count questions here on SO, e.g. stackoverflow.com/questions/29122394/… It's basically the same, just do it for the characters, i.e.flatMap
each word to the chars in an additional step.
– AKSW
Nov 23 '18 at 7:22
Possible duplicate of Checking for Alphabets in a String in java
– Arnaud
Nov 23 '18 at 7:26
add a comment |
Please look at the word count questions here on SO, e.g. stackoverflow.com/questions/29122394/… It's basically the same, just do it for the characters, i.e.flatMap
each word to the chars in an additional step.
– AKSW
Nov 23 '18 at 7:22
Possible duplicate of Checking for Alphabets in a String in java
– Arnaud
Nov 23 '18 at 7:26
Please look at the word count questions here on SO, e.g. stackoverflow.com/questions/29122394/… It's basically the same, just do it for the characters, i.e.
flatMap
each word to the chars in an additional step.– AKSW
Nov 23 '18 at 7:22
Please look at the word count questions here on SO, e.g. stackoverflow.com/questions/29122394/… It's basically the same, just do it for the characters, i.e.
flatMap
each word to the chars in an additional step.– AKSW
Nov 23 '18 at 7:22
Possible duplicate of Checking for Alphabets in a String in java
– Arnaud
Nov 23 '18 at 7:26
Possible duplicate of Checking for Alphabets in a String in java
– Arnaud
Nov 23 '18 at 7:26
add a comment |
2 Answers
2
active
oldest
votes
You can try this. I am using library : fontbox-2.0.12 and pdfbox-2.0.12 and commons-logging-1.2
try {
PDDocument doc = PDDocument.load(new File("E:\project-test\scloud\test\src\main\resources\Shadip_Banik.pdf"));
String text = new PDFTextStripper().getText(doc);
sourceCode = text.replace("-", "").replace(".", "");
String words = sourceCode.split(" ");
amountOfWords = amountOfWords + words.length;
StringBuilder builder = new StringBuilder();
for (String word : words) {
amountOfChars = amountOfChars + word.length();
Pattern pattern = Pattern.compile("[a-zA-Z]");
Matcher matcher = pattern.matcher(word);
//System.out.println("MatchesCount "+matcher.group());
while (matcher.find()) {
builder.append(matcher.group());
}
}
String allData = builder.toString();
System.out.println(builder.toString());
int total = 0;
for (int i=0;i<allAlphabate.length();i++)
{
int alphabateCount = 0;
Pattern pattern = Pattern.compile(Character.toString(allAlphabate.charAt(i)));
Matcher matcher = pattern.matcher(allData);
while (matcher.find()) {
alphabateCount++;
}
total+=alphabateCount;
System.out.println(allAlphabate.charAt(i) +" : "+alphabateCount);
}
if(total == builder.toString().length())
{
System.out.println("Yes -------------------------------------------------");
}
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 (Exception ert) {
}
But If you want to calculate uppercase and lowercase together use this code
for (String word : words) {
amountOfChars = amountOfChars + word.length();
Pattern pattern = Pattern.compile("[a-zA-Z]");
Matcher matcher = pattern.matcher(word.toLowerCase());
//System.out.println("MatchesCount "+matcher.group());
while (matcher.find()) {
builder.append(matcher.group());
}
}
String allData = builder.toString();
int total = 0;
for (int i=0;i<allAlphabate.length();i++)
{
int alphabateCount = 0;
Pattern pattern = Pattern.compile(Character.toString(allAlphabate.charAt(i)));
Matcher matcher = pattern.matcher(allData.toLowerCase());
while (matcher.find()) {
alphabateCount++;
}
total+=alphabateCount;
System.out.println(allAlphabate.charAt(i) +" : "+alphabateCount);
}
if(total == builder.toString().length())
{
System.out.println("Yes -------------------------------------------------");
}
1
the output is weird, it calculates the word occur how many times, not alphabet(a-z)
– Choy
Nov 23 '18 at 9:24
Check now. Probably it will that you desire
– flopcoder
Nov 23 '18 at 9:34
yaya, it looks what I want now, but these codes didn't count uppercase and lowercase together, right?
– Choy
Nov 23 '18 at 9:45
its calculate uppercase and lowercase toghether. you can do it some change on this code
– flopcoder
Nov 23 '18 at 9:46
change this to String allAlphabate = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; and then remove tolowercase function in sevaral places
– flopcoder
Nov 23 '18 at 9:48
|
show 4 more comments
try this:
String str = new PDFTextStripper().getText(doc);
int len = str.length();
Map<Character, Integer> numOfChars = new HashMap<Character, Integer>(Math.min(len, 26));
for (int i = 0; i < len; ++i)//pre increment
{
char alphabet = str.charAt(i);
if (!numOfChars.containsKey(alphabet ))
{
numOfChars.put(alphabet , 1);
}
else
{
numOfChars.put(alphabet , numOfChars.get(alphabet ) + 1);
}
}
System.out.println(numOfChars);
Source
these code basically print not only alphabet, it print symbol also like "=9, !=2
– Choy
Nov 23 '18 at 9:26
@Choy it is giving output like this{a=3, s=1, d=1, w=1, h=1, i=2, m=1, n=4}
what else do you want?
– nimi0112
Nov 23 '18 at 10:12
no the output contain others else
– Choy
Nov 23 '18 at 10:31
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%2f53442215%2fhow-to-count-the-number-of-alphabet-in-a-pdf-file-in-java%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
You can try this. I am using library : fontbox-2.0.12 and pdfbox-2.0.12 and commons-logging-1.2
try {
PDDocument doc = PDDocument.load(new File("E:\project-test\scloud\test\src\main\resources\Shadip_Banik.pdf"));
String text = new PDFTextStripper().getText(doc);
sourceCode = text.replace("-", "").replace(".", "");
String words = sourceCode.split(" ");
amountOfWords = amountOfWords + words.length;
StringBuilder builder = new StringBuilder();
for (String word : words) {
amountOfChars = amountOfChars + word.length();
Pattern pattern = Pattern.compile("[a-zA-Z]");
Matcher matcher = pattern.matcher(word);
//System.out.println("MatchesCount "+matcher.group());
while (matcher.find()) {
builder.append(matcher.group());
}
}
String allData = builder.toString();
System.out.println(builder.toString());
int total = 0;
for (int i=0;i<allAlphabate.length();i++)
{
int alphabateCount = 0;
Pattern pattern = Pattern.compile(Character.toString(allAlphabate.charAt(i)));
Matcher matcher = pattern.matcher(allData);
while (matcher.find()) {
alphabateCount++;
}
total+=alphabateCount;
System.out.println(allAlphabate.charAt(i) +" : "+alphabateCount);
}
if(total == builder.toString().length())
{
System.out.println("Yes -------------------------------------------------");
}
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 (Exception ert) {
}
But If you want to calculate uppercase and lowercase together use this code
for (String word : words) {
amountOfChars = amountOfChars + word.length();
Pattern pattern = Pattern.compile("[a-zA-Z]");
Matcher matcher = pattern.matcher(word.toLowerCase());
//System.out.println("MatchesCount "+matcher.group());
while (matcher.find()) {
builder.append(matcher.group());
}
}
String allData = builder.toString();
int total = 0;
for (int i=0;i<allAlphabate.length();i++)
{
int alphabateCount = 0;
Pattern pattern = Pattern.compile(Character.toString(allAlphabate.charAt(i)));
Matcher matcher = pattern.matcher(allData.toLowerCase());
while (matcher.find()) {
alphabateCount++;
}
total+=alphabateCount;
System.out.println(allAlphabate.charAt(i) +" : "+alphabateCount);
}
if(total == builder.toString().length())
{
System.out.println("Yes -------------------------------------------------");
}
1
the output is weird, it calculates the word occur how many times, not alphabet(a-z)
– Choy
Nov 23 '18 at 9:24
Check now. Probably it will that you desire
– flopcoder
Nov 23 '18 at 9:34
yaya, it looks what I want now, but these codes didn't count uppercase and lowercase together, right?
– Choy
Nov 23 '18 at 9:45
its calculate uppercase and lowercase toghether. you can do it some change on this code
– flopcoder
Nov 23 '18 at 9:46
change this to String allAlphabate = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; and then remove tolowercase function in sevaral places
– flopcoder
Nov 23 '18 at 9:48
|
show 4 more comments
You can try this. I am using library : fontbox-2.0.12 and pdfbox-2.0.12 and commons-logging-1.2
try {
PDDocument doc = PDDocument.load(new File("E:\project-test\scloud\test\src\main\resources\Shadip_Banik.pdf"));
String text = new PDFTextStripper().getText(doc);
sourceCode = text.replace("-", "").replace(".", "");
String words = sourceCode.split(" ");
amountOfWords = amountOfWords + words.length;
StringBuilder builder = new StringBuilder();
for (String word : words) {
amountOfChars = amountOfChars + word.length();
Pattern pattern = Pattern.compile("[a-zA-Z]");
Matcher matcher = pattern.matcher(word);
//System.out.println("MatchesCount "+matcher.group());
while (matcher.find()) {
builder.append(matcher.group());
}
}
String allData = builder.toString();
System.out.println(builder.toString());
int total = 0;
for (int i=0;i<allAlphabate.length();i++)
{
int alphabateCount = 0;
Pattern pattern = Pattern.compile(Character.toString(allAlphabate.charAt(i)));
Matcher matcher = pattern.matcher(allData);
while (matcher.find()) {
alphabateCount++;
}
total+=alphabateCount;
System.out.println(allAlphabate.charAt(i) +" : "+alphabateCount);
}
if(total == builder.toString().length())
{
System.out.println("Yes -------------------------------------------------");
}
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 (Exception ert) {
}
But If you want to calculate uppercase and lowercase together use this code
for (String word : words) {
amountOfChars = amountOfChars + word.length();
Pattern pattern = Pattern.compile("[a-zA-Z]");
Matcher matcher = pattern.matcher(word.toLowerCase());
//System.out.println("MatchesCount "+matcher.group());
while (matcher.find()) {
builder.append(matcher.group());
}
}
String allData = builder.toString();
int total = 0;
for (int i=0;i<allAlphabate.length();i++)
{
int alphabateCount = 0;
Pattern pattern = Pattern.compile(Character.toString(allAlphabate.charAt(i)));
Matcher matcher = pattern.matcher(allData.toLowerCase());
while (matcher.find()) {
alphabateCount++;
}
total+=alphabateCount;
System.out.println(allAlphabate.charAt(i) +" : "+alphabateCount);
}
if(total == builder.toString().length())
{
System.out.println("Yes -------------------------------------------------");
}
1
the output is weird, it calculates the word occur how many times, not alphabet(a-z)
– Choy
Nov 23 '18 at 9:24
Check now. Probably it will that you desire
– flopcoder
Nov 23 '18 at 9:34
yaya, it looks what I want now, but these codes didn't count uppercase and lowercase together, right?
– Choy
Nov 23 '18 at 9:45
its calculate uppercase and lowercase toghether. you can do it some change on this code
– flopcoder
Nov 23 '18 at 9:46
change this to String allAlphabate = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; and then remove tolowercase function in sevaral places
– flopcoder
Nov 23 '18 at 9:48
|
show 4 more comments
You can try this. I am using library : fontbox-2.0.12 and pdfbox-2.0.12 and commons-logging-1.2
try {
PDDocument doc = PDDocument.load(new File("E:\project-test\scloud\test\src\main\resources\Shadip_Banik.pdf"));
String text = new PDFTextStripper().getText(doc);
sourceCode = text.replace("-", "").replace(".", "");
String words = sourceCode.split(" ");
amountOfWords = amountOfWords + words.length;
StringBuilder builder = new StringBuilder();
for (String word : words) {
amountOfChars = amountOfChars + word.length();
Pattern pattern = Pattern.compile("[a-zA-Z]");
Matcher matcher = pattern.matcher(word);
//System.out.println("MatchesCount "+matcher.group());
while (matcher.find()) {
builder.append(matcher.group());
}
}
String allData = builder.toString();
System.out.println(builder.toString());
int total = 0;
for (int i=0;i<allAlphabate.length();i++)
{
int alphabateCount = 0;
Pattern pattern = Pattern.compile(Character.toString(allAlphabate.charAt(i)));
Matcher matcher = pattern.matcher(allData);
while (matcher.find()) {
alphabateCount++;
}
total+=alphabateCount;
System.out.println(allAlphabate.charAt(i) +" : "+alphabateCount);
}
if(total == builder.toString().length())
{
System.out.println("Yes -------------------------------------------------");
}
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 (Exception ert) {
}
But If you want to calculate uppercase and lowercase together use this code
for (String word : words) {
amountOfChars = amountOfChars + word.length();
Pattern pattern = Pattern.compile("[a-zA-Z]");
Matcher matcher = pattern.matcher(word.toLowerCase());
//System.out.println("MatchesCount "+matcher.group());
while (matcher.find()) {
builder.append(matcher.group());
}
}
String allData = builder.toString();
int total = 0;
for (int i=0;i<allAlphabate.length();i++)
{
int alphabateCount = 0;
Pattern pattern = Pattern.compile(Character.toString(allAlphabate.charAt(i)));
Matcher matcher = pattern.matcher(allData.toLowerCase());
while (matcher.find()) {
alphabateCount++;
}
total+=alphabateCount;
System.out.println(allAlphabate.charAt(i) +" : "+alphabateCount);
}
if(total == builder.toString().length())
{
System.out.println("Yes -------------------------------------------------");
}
You can try this. I am using library : fontbox-2.0.12 and pdfbox-2.0.12 and commons-logging-1.2
try {
PDDocument doc = PDDocument.load(new File("E:\project-test\scloud\test\src\main\resources\Shadip_Banik.pdf"));
String text = new PDFTextStripper().getText(doc);
sourceCode = text.replace("-", "").replace(".", "");
String words = sourceCode.split(" ");
amountOfWords = amountOfWords + words.length;
StringBuilder builder = new StringBuilder();
for (String word : words) {
amountOfChars = amountOfChars + word.length();
Pattern pattern = Pattern.compile("[a-zA-Z]");
Matcher matcher = pattern.matcher(word);
//System.out.println("MatchesCount "+matcher.group());
while (matcher.find()) {
builder.append(matcher.group());
}
}
String allData = builder.toString();
System.out.println(builder.toString());
int total = 0;
for (int i=0;i<allAlphabate.length();i++)
{
int alphabateCount = 0;
Pattern pattern = Pattern.compile(Character.toString(allAlphabate.charAt(i)));
Matcher matcher = pattern.matcher(allData);
while (matcher.find()) {
alphabateCount++;
}
total+=alphabateCount;
System.out.println(allAlphabate.charAt(i) +" : "+alphabateCount);
}
if(total == builder.toString().length())
{
System.out.println("Yes -------------------------------------------------");
}
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 (Exception ert) {
}
But If you want to calculate uppercase and lowercase together use this code
for (String word : words) {
amountOfChars = amountOfChars + word.length();
Pattern pattern = Pattern.compile("[a-zA-Z]");
Matcher matcher = pattern.matcher(word.toLowerCase());
//System.out.println("MatchesCount "+matcher.group());
while (matcher.find()) {
builder.append(matcher.group());
}
}
String allData = builder.toString();
int total = 0;
for (int i=0;i<allAlphabate.length();i++)
{
int alphabateCount = 0;
Pattern pattern = Pattern.compile(Character.toString(allAlphabate.charAt(i)));
Matcher matcher = pattern.matcher(allData.toLowerCase());
while (matcher.find()) {
alphabateCount++;
}
total+=alphabateCount;
System.out.println(allAlphabate.charAt(i) +" : "+alphabateCount);
}
if(total == builder.toString().length())
{
System.out.println("Yes -------------------------------------------------");
}
edited Nov 23 '18 at 12:11
answered Nov 23 '18 at 9:08
flopcoderflopcoder
745512
745512
1
the output is weird, it calculates the word occur how many times, not alphabet(a-z)
– Choy
Nov 23 '18 at 9:24
Check now. Probably it will that you desire
– flopcoder
Nov 23 '18 at 9:34
yaya, it looks what I want now, but these codes didn't count uppercase and lowercase together, right?
– Choy
Nov 23 '18 at 9:45
its calculate uppercase and lowercase toghether. you can do it some change on this code
– flopcoder
Nov 23 '18 at 9:46
change this to String allAlphabate = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; and then remove tolowercase function in sevaral places
– flopcoder
Nov 23 '18 at 9:48
|
show 4 more comments
1
the output is weird, it calculates the word occur how many times, not alphabet(a-z)
– Choy
Nov 23 '18 at 9:24
Check now. Probably it will that you desire
– flopcoder
Nov 23 '18 at 9:34
yaya, it looks what I want now, but these codes didn't count uppercase and lowercase together, right?
– Choy
Nov 23 '18 at 9:45
its calculate uppercase and lowercase toghether. you can do it some change on this code
– flopcoder
Nov 23 '18 at 9:46
change this to String allAlphabate = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; and then remove tolowercase function in sevaral places
– flopcoder
Nov 23 '18 at 9:48
1
1
the output is weird, it calculates the word occur how many times, not alphabet(a-z)
– Choy
Nov 23 '18 at 9:24
the output is weird, it calculates the word occur how many times, not alphabet(a-z)
– Choy
Nov 23 '18 at 9:24
Check now. Probably it will that you desire
– flopcoder
Nov 23 '18 at 9:34
Check now. Probably it will that you desire
– flopcoder
Nov 23 '18 at 9:34
yaya, it looks what I want now, but these codes didn't count uppercase and lowercase together, right?
– Choy
Nov 23 '18 at 9:45
yaya, it looks what I want now, but these codes didn't count uppercase and lowercase together, right?
– Choy
Nov 23 '18 at 9:45
its calculate uppercase and lowercase toghether. you can do it some change on this code
– flopcoder
Nov 23 '18 at 9:46
its calculate uppercase and lowercase toghether. you can do it some change on this code
– flopcoder
Nov 23 '18 at 9:46
change this to String allAlphabate = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; and then remove tolowercase function in sevaral places
– flopcoder
Nov 23 '18 at 9:48
change this to String allAlphabate = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; and then remove tolowercase function in sevaral places
– flopcoder
Nov 23 '18 at 9:48
|
show 4 more comments
try this:
String str = new PDFTextStripper().getText(doc);
int len = str.length();
Map<Character, Integer> numOfChars = new HashMap<Character, Integer>(Math.min(len, 26));
for (int i = 0; i < len; ++i)//pre increment
{
char alphabet = str.charAt(i);
if (!numOfChars.containsKey(alphabet ))
{
numOfChars.put(alphabet , 1);
}
else
{
numOfChars.put(alphabet , numOfChars.get(alphabet ) + 1);
}
}
System.out.println(numOfChars);
Source
these code basically print not only alphabet, it print symbol also like "=9, !=2
– Choy
Nov 23 '18 at 9:26
@Choy it is giving output like this{a=3, s=1, d=1, w=1, h=1, i=2, m=1, n=4}
what else do you want?
– nimi0112
Nov 23 '18 at 10:12
no the output contain others else
– Choy
Nov 23 '18 at 10:31
add a comment |
try this:
String str = new PDFTextStripper().getText(doc);
int len = str.length();
Map<Character, Integer> numOfChars = new HashMap<Character, Integer>(Math.min(len, 26));
for (int i = 0; i < len; ++i)//pre increment
{
char alphabet = str.charAt(i);
if (!numOfChars.containsKey(alphabet ))
{
numOfChars.put(alphabet , 1);
}
else
{
numOfChars.put(alphabet , numOfChars.get(alphabet ) + 1);
}
}
System.out.println(numOfChars);
Source
these code basically print not only alphabet, it print symbol also like "=9, !=2
– Choy
Nov 23 '18 at 9:26
@Choy it is giving output like this{a=3, s=1, d=1, w=1, h=1, i=2, m=1, n=4}
what else do you want?
– nimi0112
Nov 23 '18 at 10:12
no the output contain others else
– Choy
Nov 23 '18 at 10:31
add a comment |
try this:
String str = new PDFTextStripper().getText(doc);
int len = str.length();
Map<Character, Integer> numOfChars = new HashMap<Character, Integer>(Math.min(len, 26));
for (int i = 0; i < len; ++i)//pre increment
{
char alphabet = str.charAt(i);
if (!numOfChars.containsKey(alphabet ))
{
numOfChars.put(alphabet , 1);
}
else
{
numOfChars.put(alphabet , numOfChars.get(alphabet ) + 1);
}
}
System.out.println(numOfChars);
Source
try this:
String str = new PDFTextStripper().getText(doc);
int len = str.length();
Map<Character, Integer> numOfChars = new HashMap<Character, Integer>(Math.min(len, 26));
for (int i = 0; i < len; ++i)//pre increment
{
char alphabet = str.charAt(i);
if (!numOfChars.containsKey(alphabet ))
{
numOfChars.put(alphabet , 1);
}
else
{
numOfChars.put(alphabet , numOfChars.get(alphabet ) + 1);
}
}
System.out.println(numOfChars);
Source
answered Nov 23 '18 at 7:49
nimi0112nimi0112
8961722
8961722
these code basically print not only alphabet, it print symbol also like "=9, !=2
– Choy
Nov 23 '18 at 9:26
@Choy it is giving output like this{a=3, s=1, d=1, w=1, h=1, i=2, m=1, n=4}
what else do you want?
– nimi0112
Nov 23 '18 at 10:12
no the output contain others else
– Choy
Nov 23 '18 at 10:31
add a comment |
these code basically print not only alphabet, it print symbol also like "=9, !=2
– Choy
Nov 23 '18 at 9:26
@Choy it is giving output like this{a=3, s=1, d=1, w=1, h=1, i=2, m=1, n=4}
what else do you want?
– nimi0112
Nov 23 '18 at 10:12
no the output contain others else
– Choy
Nov 23 '18 at 10:31
these code basically print not only alphabet, it print symbol also like "=9, !=2
– Choy
Nov 23 '18 at 9:26
these code basically print not only alphabet, it print symbol also like "=9, !=2
– Choy
Nov 23 '18 at 9:26
@Choy it is giving output like this
{a=3, s=1, d=1, w=1, h=1, i=2, m=1, n=4}
what else do you want?– nimi0112
Nov 23 '18 at 10:12
@Choy it is giving output like this
{a=3, s=1, d=1, w=1, h=1, i=2, m=1, n=4}
what else do you want?– nimi0112
Nov 23 '18 at 10:12
no the output contain others else
– Choy
Nov 23 '18 at 10:31
no the output contain others else
– Choy
Nov 23 '18 at 10:31
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%2f53442215%2fhow-to-count-the-number-of-alphabet-in-a-pdf-file-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
Please look at the word count questions here on SO, e.g. stackoverflow.com/questions/29122394/… It's basically the same, just do it for the characters, i.e.
flatMap
each word to the chars in an additional step.– AKSW
Nov 23 '18 at 7:22
Possible duplicate of Checking for Alphabets in a String in java
– Arnaud
Nov 23 '18 at 7:26