How to make a frequency histogram in java









up vote
0
down vote

favorite












I am trying to create a code that reads through a file and prints out the following:



  • number of lines of the file

  • the frequency of each letter

  • the frequency of each non-alphabetic character

I want to create a histogram for the frequency of the letters and numbers but I can't seem to find a solution around it. If anything i would want the output to look like this:



A ***** - 5
B *** - 3
C ******* - 7


My output looks like this:



*********************
*********************
*********************
A 263
B 130
C 50


etc.










share|improve this question























  • What do the contents of your file look like? I'd be able to help you if I knew what you are trying to read from.
    – Ishaan
    Nov 11 at 19:53











  • It is just a file with many paragraphs of information - imagine copying and pasting a couple paragraphs from a Wikipedia page
    – masterandomguy
    Nov 11 at 19:54










  • Ohhh. I misread it. Now I get it. You are trying to read the number of letters. What problems are you running into?
    – Ishaan
    Nov 11 at 19:55










  • and what does not work? You could use Java 8 stream API and look for the word count example. It's just one more step to flat map each word to the characters and then get the character count
    – AKSW
    Nov 11 at 19:55







  • 1




    Don't print the stars while looping throught the charracters of the file. Print them at the end, after the loop, when you print the number of occurrences of each character.
    – JB Nizet
    Nov 11 at 20:06














up vote
0
down vote

favorite












I am trying to create a code that reads through a file and prints out the following:



  • number of lines of the file

  • the frequency of each letter

  • the frequency of each non-alphabetic character

I want to create a histogram for the frequency of the letters and numbers but I can't seem to find a solution around it. If anything i would want the output to look like this:



A ***** - 5
B *** - 3
C ******* - 7


My output looks like this:



*********************
*********************
*********************
A 263
B 130
C 50


etc.










share|improve this question























  • What do the contents of your file look like? I'd be able to help you if I knew what you are trying to read from.
    – Ishaan
    Nov 11 at 19:53











  • It is just a file with many paragraphs of information - imagine copying and pasting a couple paragraphs from a Wikipedia page
    – masterandomguy
    Nov 11 at 19:54










  • Ohhh. I misread it. Now I get it. You are trying to read the number of letters. What problems are you running into?
    – Ishaan
    Nov 11 at 19:55










  • and what does not work? You could use Java 8 stream API and look for the word count example. It's just one more step to flat map each word to the characters and then get the character count
    – AKSW
    Nov 11 at 19:55







  • 1




    Don't print the stars while looping throught the charracters of the file. Print them at the end, after the loop, when you print the number of occurrences of each character.
    – JB Nizet
    Nov 11 at 20:06












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am trying to create a code that reads through a file and prints out the following:



  • number of lines of the file

  • the frequency of each letter

  • the frequency of each non-alphabetic character

I want to create a histogram for the frequency of the letters and numbers but I can't seem to find a solution around it. If anything i would want the output to look like this:



A ***** - 5
B *** - 3
C ******* - 7


My output looks like this:



*********************
*********************
*********************
A 263
B 130
C 50


etc.










share|improve this question















I am trying to create a code that reads through a file and prints out the following:



  • number of lines of the file

  • the frequency of each letter

  • the frequency of each non-alphabetic character

I want to create a histogram for the frequency of the letters and numbers but I can't seem to find a solution around it. If anything i would want the output to look like this:



A ***** - 5
B *** - 3
C ******* - 7


My output looks like this:



*********************
*********************
*********************
A 263
B 130
C 50


etc.







java histogram frequency






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 20:43

























asked Nov 11 at 19:50









masterandomguy

32




32











  • What do the contents of your file look like? I'd be able to help you if I knew what you are trying to read from.
    – Ishaan
    Nov 11 at 19:53











  • It is just a file with many paragraphs of information - imagine copying and pasting a couple paragraphs from a Wikipedia page
    – masterandomguy
    Nov 11 at 19:54










  • Ohhh. I misread it. Now I get it. You are trying to read the number of letters. What problems are you running into?
    – Ishaan
    Nov 11 at 19:55










  • and what does not work? You could use Java 8 stream API and look for the word count example. It's just one more step to flat map each word to the characters and then get the character count
    – AKSW
    Nov 11 at 19:55







  • 1




    Don't print the stars while looping throught the charracters of the file. Print them at the end, after the loop, when you print the number of occurrences of each character.
    – JB Nizet
    Nov 11 at 20:06
















  • What do the contents of your file look like? I'd be able to help you if I knew what you are trying to read from.
    – Ishaan
    Nov 11 at 19:53











  • It is just a file with many paragraphs of information - imagine copying and pasting a couple paragraphs from a Wikipedia page
    – masterandomguy
    Nov 11 at 19:54










  • Ohhh. I misread it. Now I get it. You are trying to read the number of letters. What problems are you running into?
    – Ishaan
    Nov 11 at 19:55










  • and what does not work? You could use Java 8 stream API and look for the word count example. It's just one more step to flat map each word to the characters and then get the character count
    – AKSW
    Nov 11 at 19:55







  • 1




    Don't print the stars while looping throught the charracters of the file. Print them at the end, after the loop, when you print the number of occurrences of each character.
    – JB Nizet
    Nov 11 at 20:06















What do the contents of your file look like? I'd be able to help you if I knew what you are trying to read from.
– Ishaan
Nov 11 at 19:53





What do the contents of your file look like? I'd be able to help you if I knew what you are trying to read from.
– Ishaan
Nov 11 at 19:53













It is just a file with many paragraphs of information - imagine copying and pasting a couple paragraphs from a Wikipedia page
– masterandomguy
Nov 11 at 19:54




It is just a file with many paragraphs of information - imagine copying and pasting a couple paragraphs from a Wikipedia page
– masterandomguy
Nov 11 at 19:54












Ohhh. I misread it. Now I get it. You are trying to read the number of letters. What problems are you running into?
– Ishaan
Nov 11 at 19:55




Ohhh. I misread it. Now I get it. You are trying to read the number of letters. What problems are you running into?
– Ishaan
Nov 11 at 19:55












and what does not work? You could use Java 8 stream API and look for the word count example. It's just one more step to flat map each word to the characters and then get the character count
– AKSW
Nov 11 at 19:55





and what does not work? You could use Java 8 stream API and look for the word count example. It's just one more step to flat map each word to the characters and then get the character count
– AKSW
Nov 11 at 19:55





1




1




Don't print the stars while looping throught the charracters of the file. Print them at the end, after the loop, when you print the number of occurrences of each character.
– JB Nizet
Nov 11 at 20:06




Don't print the stars while looping throught the charracters of the file. Print them at the end, after the loop, when you print the number of occurrences of each character.
– JB Nizet
Nov 11 at 20:06












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










This is how you do the task. It counts the number of lower case letters and prints the stars as well in addition to the frequency, just as you wanted.



Here is the general code (paragraph is the string that contains the content of your file):



int lettercount = new int[26];
for(int i = 0; i < 26; i++)
//Set every single number in the array to 0.
lettercount[i] = 0;


for(char s : paragraph.toCharArray())
int converted = (int) s;
converted -= 97;
if(converted >=0 && converted <=25)
lettercount[converted] += 1;



//Print out the letter with the frequencies.
for(int i = 0; i < 26; i++)
char convertback = (char) (i+97);
String stars = "";
for(int j = 0; j < lettercount[i]; j++)
stars += "*";

System.out.println(convertback + " " + stars + " - " + lettercount[i]);



This should work for lowercase letters. If you want to do uppercase letters, lettercount should be 52 elements long. You would also have to check if converted (in the second for loop), after subtracting 97 is negative, and if it is, you would add back 58.



I hope this helps! If you have any problems, just comment below.






share|improve this answer






















  • Thank you so much! I did a little bit of modifying and it worked, much appreciated!
    – masterandomguy
    Nov 11 at 20:31










  • You are most welcome! If you found this answer useful, please upvote it or accept it as an answer therefore others can refer to it if they face similar issues.
    – Ishaan
    Nov 11 at 20:39










  • Thank you and I am glad that I was able to help you!
    – Ishaan
    Nov 11 at 20:44










  • A few things. First, this looks an awful lot like a homework problem, and I'd encourage @masterandomguy to do his own work. Second, this solution relies on specifics of how characters are assigned numbers (the - 97 bit) and really isn't a very good demonstration. It doesn't handle non-latin characters at all, é, for example, won't be counted correctly. I would suggest using a Map instead, and maybe a set of default characters that you want reported.
    – Yona Appletree
    Nov 11 at 20:53










  • Yes, as Yona said I was initially thinking of using a map with a Character key and an Integer value. The way that this would work is you would get the character and check if there is a pair in your map that has the same key. If not, you would add the Character and the value of 1. If the pair did exist, you would get the value associated with that character, increment it, and then add it back to the map. For your purposes, counting English letters, I think the current solution is sufficient. Maps would be useful if you wanted to count non- English letters such as á or symbols like '!'.
    – Ishaan
    Nov 11 at 21:00










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',
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%2f53252568%2fhow-to-make-a-frequency-histogram-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








up vote
0
down vote



accepted










This is how you do the task. It counts the number of lower case letters and prints the stars as well in addition to the frequency, just as you wanted.



Here is the general code (paragraph is the string that contains the content of your file):



int lettercount = new int[26];
for(int i = 0; i < 26; i++)
//Set every single number in the array to 0.
lettercount[i] = 0;


for(char s : paragraph.toCharArray())
int converted = (int) s;
converted -= 97;
if(converted >=0 && converted <=25)
lettercount[converted] += 1;



//Print out the letter with the frequencies.
for(int i = 0; i < 26; i++)
char convertback = (char) (i+97);
String stars = "";
for(int j = 0; j < lettercount[i]; j++)
stars += "*";

System.out.println(convertback + " " + stars + " - " + lettercount[i]);



This should work for lowercase letters. If you want to do uppercase letters, lettercount should be 52 elements long. You would also have to check if converted (in the second for loop), after subtracting 97 is negative, and if it is, you would add back 58.



I hope this helps! If you have any problems, just comment below.






share|improve this answer






















  • Thank you so much! I did a little bit of modifying and it worked, much appreciated!
    – masterandomguy
    Nov 11 at 20:31










  • You are most welcome! If you found this answer useful, please upvote it or accept it as an answer therefore others can refer to it if they face similar issues.
    – Ishaan
    Nov 11 at 20:39










  • Thank you and I am glad that I was able to help you!
    – Ishaan
    Nov 11 at 20:44










  • A few things. First, this looks an awful lot like a homework problem, and I'd encourage @masterandomguy to do his own work. Second, this solution relies on specifics of how characters are assigned numbers (the - 97 bit) and really isn't a very good demonstration. It doesn't handle non-latin characters at all, é, for example, won't be counted correctly. I would suggest using a Map instead, and maybe a set of default characters that you want reported.
    – Yona Appletree
    Nov 11 at 20:53










  • Yes, as Yona said I was initially thinking of using a map with a Character key and an Integer value. The way that this would work is you would get the character and check if there is a pair in your map that has the same key. If not, you would add the Character and the value of 1. If the pair did exist, you would get the value associated with that character, increment it, and then add it back to the map. For your purposes, counting English letters, I think the current solution is sufficient. Maps would be useful if you wanted to count non- English letters such as á or symbols like '!'.
    – Ishaan
    Nov 11 at 21:00














up vote
0
down vote



accepted










This is how you do the task. It counts the number of lower case letters and prints the stars as well in addition to the frequency, just as you wanted.



Here is the general code (paragraph is the string that contains the content of your file):



int lettercount = new int[26];
for(int i = 0; i < 26; i++)
//Set every single number in the array to 0.
lettercount[i] = 0;


for(char s : paragraph.toCharArray())
int converted = (int) s;
converted -= 97;
if(converted >=0 && converted <=25)
lettercount[converted] += 1;



//Print out the letter with the frequencies.
for(int i = 0; i < 26; i++)
char convertback = (char) (i+97);
String stars = "";
for(int j = 0; j < lettercount[i]; j++)
stars += "*";

System.out.println(convertback + " " + stars + " - " + lettercount[i]);



This should work for lowercase letters. If you want to do uppercase letters, lettercount should be 52 elements long. You would also have to check if converted (in the second for loop), after subtracting 97 is negative, and if it is, you would add back 58.



I hope this helps! If you have any problems, just comment below.






share|improve this answer






















  • Thank you so much! I did a little bit of modifying and it worked, much appreciated!
    – masterandomguy
    Nov 11 at 20:31










  • You are most welcome! If you found this answer useful, please upvote it or accept it as an answer therefore others can refer to it if they face similar issues.
    – Ishaan
    Nov 11 at 20:39










  • Thank you and I am glad that I was able to help you!
    – Ishaan
    Nov 11 at 20:44










  • A few things. First, this looks an awful lot like a homework problem, and I'd encourage @masterandomguy to do his own work. Second, this solution relies on specifics of how characters are assigned numbers (the - 97 bit) and really isn't a very good demonstration. It doesn't handle non-latin characters at all, é, for example, won't be counted correctly. I would suggest using a Map instead, and maybe a set of default characters that you want reported.
    – Yona Appletree
    Nov 11 at 20:53










  • Yes, as Yona said I was initially thinking of using a map with a Character key and an Integer value. The way that this would work is you would get the character and check if there is a pair in your map that has the same key. If not, you would add the Character and the value of 1. If the pair did exist, you would get the value associated with that character, increment it, and then add it back to the map. For your purposes, counting English letters, I think the current solution is sufficient. Maps would be useful if you wanted to count non- English letters such as á or symbols like '!'.
    – Ishaan
    Nov 11 at 21:00












up vote
0
down vote



accepted







up vote
0
down vote



accepted






This is how you do the task. It counts the number of lower case letters and prints the stars as well in addition to the frequency, just as you wanted.



Here is the general code (paragraph is the string that contains the content of your file):



int lettercount = new int[26];
for(int i = 0; i < 26; i++)
//Set every single number in the array to 0.
lettercount[i] = 0;


for(char s : paragraph.toCharArray())
int converted = (int) s;
converted -= 97;
if(converted >=0 && converted <=25)
lettercount[converted] += 1;



//Print out the letter with the frequencies.
for(int i = 0; i < 26; i++)
char convertback = (char) (i+97);
String stars = "";
for(int j = 0; j < lettercount[i]; j++)
stars += "*";

System.out.println(convertback + " " + stars + " - " + lettercount[i]);



This should work for lowercase letters. If you want to do uppercase letters, lettercount should be 52 elements long. You would also have to check if converted (in the second for loop), after subtracting 97 is negative, and if it is, you would add back 58.



I hope this helps! If you have any problems, just comment below.






share|improve this answer














This is how you do the task. It counts the number of lower case letters and prints the stars as well in addition to the frequency, just as you wanted.



Here is the general code (paragraph is the string that contains the content of your file):



int lettercount = new int[26];
for(int i = 0; i < 26; i++)
//Set every single number in the array to 0.
lettercount[i] = 0;


for(char s : paragraph.toCharArray())
int converted = (int) s;
converted -= 97;
if(converted >=0 && converted <=25)
lettercount[converted] += 1;



//Print out the letter with the frequencies.
for(int i = 0; i < 26; i++)
char convertback = (char) (i+97);
String stars = "";
for(int j = 0; j < lettercount[i]; j++)
stars += "*";

System.out.println(convertback + " " + stars + " - " + lettercount[i]);



This should work for lowercase letters. If you want to do uppercase letters, lettercount should be 52 elements long. You would also have to check if converted (in the second for loop), after subtracting 97 is negative, and if it is, you would add back 58.



I hope this helps! If you have any problems, just comment below.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 11 at 20:12

























answered Nov 11 at 20:06









Ishaan

7671417




7671417











  • Thank you so much! I did a little bit of modifying and it worked, much appreciated!
    – masterandomguy
    Nov 11 at 20:31










  • You are most welcome! If you found this answer useful, please upvote it or accept it as an answer therefore others can refer to it if they face similar issues.
    – Ishaan
    Nov 11 at 20:39










  • Thank you and I am glad that I was able to help you!
    – Ishaan
    Nov 11 at 20:44










  • A few things. First, this looks an awful lot like a homework problem, and I'd encourage @masterandomguy to do his own work. Second, this solution relies on specifics of how characters are assigned numbers (the - 97 bit) and really isn't a very good demonstration. It doesn't handle non-latin characters at all, é, for example, won't be counted correctly. I would suggest using a Map instead, and maybe a set of default characters that you want reported.
    – Yona Appletree
    Nov 11 at 20:53










  • Yes, as Yona said I was initially thinking of using a map with a Character key and an Integer value. The way that this would work is you would get the character and check if there is a pair in your map that has the same key. If not, you would add the Character and the value of 1. If the pair did exist, you would get the value associated with that character, increment it, and then add it back to the map. For your purposes, counting English letters, I think the current solution is sufficient. Maps would be useful if you wanted to count non- English letters such as á or symbols like '!'.
    – Ishaan
    Nov 11 at 21:00
















  • Thank you so much! I did a little bit of modifying and it worked, much appreciated!
    – masterandomguy
    Nov 11 at 20:31










  • You are most welcome! If you found this answer useful, please upvote it or accept it as an answer therefore others can refer to it if they face similar issues.
    – Ishaan
    Nov 11 at 20:39










  • Thank you and I am glad that I was able to help you!
    – Ishaan
    Nov 11 at 20:44










  • A few things. First, this looks an awful lot like a homework problem, and I'd encourage @masterandomguy to do his own work. Second, this solution relies on specifics of how characters are assigned numbers (the - 97 bit) and really isn't a very good demonstration. It doesn't handle non-latin characters at all, é, for example, won't be counted correctly. I would suggest using a Map instead, and maybe a set of default characters that you want reported.
    – Yona Appletree
    Nov 11 at 20:53










  • Yes, as Yona said I was initially thinking of using a map with a Character key and an Integer value. The way that this would work is you would get the character and check if there is a pair in your map that has the same key. If not, you would add the Character and the value of 1. If the pair did exist, you would get the value associated with that character, increment it, and then add it back to the map. For your purposes, counting English letters, I think the current solution is sufficient. Maps would be useful if you wanted to count non- English letters such as á or symbols like '!'.
    – Ishaan
    Nov 11 at 21:00















Thank you so much! I did a little bit of modifying and it worked, much appreciated!
– masterandomguy
Nov 11 at 20:31




Thank you so much! I did a little bit of modifying and it worked, much appreciated!
– masterandomguy
Nov 11 at 20:31












You are most welcome! If you found this answer useful, please upvote it or accept it as an answer therefore others can refer to it if they face similar issues.
– Ishaan
Nov 11 at 20:39




You are most welcome! If you found this answer useful, please upvote it or accept it as an answer therefore others can refer to it if they face similar issues.
– Ishaan
Nov 11 at 20:39












Thank you and I am glad that I was able to help you!
– Ishaan
Nov 11 at 20:44




Thank you and I am glad that I was able to help you!
– Ishaan
Nov 11 at 20:44












A few things. First, this looks an awful lot like a homework problem, and I'd encourage @masterandomguy to do his own work. Second, this solution relies on specifics of how characters are assigned numbers (the - 97 bit) and really isn't a very good demonstration. It doesn't handle non-latin characters at all, é, for example, won't be counted correctly. I would suggest using a Map instead, and maybe a set of default characters that you want reported.
– Yona Appletree
Nov 11 at 20:53




A few things. First, this looks an awful lot like a homework problem, and I'd encourage @masterandomguy to do his own work. Second, this solution relies on specifics of how characters are assigned numbers (the - 97 bit) and really isn't a very good demonstration. It doesn't handle non-latin characters at all, é, for example, won't be counted correctly. I would suggest using a Map instead, and maybe a set of default characters that you want reported.
– Yona Appletree
Nov 11 at 20:53












Yes, as Yona said I was initially thinking of using a map with a Character key and an Integer value. The way that this would work is you would get the character and check if there is a pair in your map that has the same key. If not, you would add the Character and the value of 1. If the pair did exist, you would get the value associated with that character, increment it, and then add it back to the map. For your purposes, counting English letters, I think the current solution is sufficient. Maps would be useful if you wanted to count non- English letters such as á or symbols like '!'.
– Ishaan
Nov 11 at 21:00




Yes, as Yona said I was initially thinking of using a map with a Character key and an Integer value. The way that this would work is you would get the character and check if there is a pair in your map that has the same key. If not, you would add the Character and the value of 1. If the pair did exist, you would get the value associated with that character, increment it, and then add it back to the map. For your purposes, counting English letters, I think the current solution is sufficient. Maps would be useful if you wanted to count non- English letters such as á or symbols like '!'.
– Ishaan
Nov 11 at 21:00

















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53252568%2fhow-to-make-a-frequency-histogram-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







這個網誌中的熱門文章

What does pagestruct do in Eviews?

Dutch intervention in Lombok and Karangasem

Channel Islands