I would like to make a java program that will read a text file and store every single character occurrence. So it will account for punctuation, letters, numbers,uppercase, lowercase ect.
Given a text file like:
Roses are red,
Violets are blue.
printing the values will look like:
R : 1
r : 3
i : 1
, : 1
[ect]
So far I am able to read a file and count words, lines, chars.
package Exercise3;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.io.*;
public class StringTokenizerDemo1
{
public static void main(String[] args) throws IOException
{
Scanner keyboard = new Scanner(System.in);
File file = new File("C://Users//guy//Desktop//Practice.txt");
Scanner inputFile = new Scanner(file);
String line, word;
StringTokenizer token;
int words = 0; //word count
int lines = 0; //line count
int chars = 0; //char count
while (inputFile.hasNext())
{
lines++; //add one to line count
line = inputFile.nextLine();
token = new StringTokenizer(line, " ");
while (token.hasMoreTokens())
{
words++; //add one word count
word = token.nextToken();
chars+= word.length(); //add to char count
}
}
}
}
I have not learned hash maps/tables or treemaps; looking for some advice on how to store all char types and their occurrences either using an array,arraylist or linkedlist.
A char
is a 16-bit unsigned value, and if you cast it to an int
, then you'll get a value between 0 and 65535. That means that you can just use an array to store your characters:
int[] charCounts = new int[65536];
and then when you want to record an occurrence of char c
:
charCounts[(int) c]++;
When you want to read off the counts:
for (int i=0; i<65536; i++)
if (charCounts[i]>0)
System.out.println((char)(i)+": "+charCounts[i]);
There is nothing to stop you doing it with a HashMap<Character,Integer>
if you want to do it as an exercise, though it's more heavyweight than it needs to be for this:
HashMap<Character,Integer> map = new HashMap<Character,Integer>();
and when you want to record the occurrence of char c
:
if (!map.containsKey(c))
map.put(c,1);
else
map.put(c,map.get(c)+1);
and when you want to read off:
for (Map.Entry<Character,Integer> entry: map.entrySet())
System.out.println(entry.getKey()+": "+entry.getValue());
Note that for all of this I've assumed you're dealing only with printable characters. If not, you'll want to do something about that when you print them out.
This will count the occurrences of each character in an array
public class jazz {
public static void main(String[] args) {
String [] arr = {"a", "b", "a","c", "d"};
HashMap<String, Integer> map = new HashMap<String,Integer>();
for (String i : arr) {
if (map.get(i) == null) {
map.put(i, 1);
} else {
map.put(i, map.get(i) + 1);
}
}
If you only want to store a limited nr of characters, where some characters are legal and others are ignored you could create an array of fixed size where the char's int value represents its index, then increment the occurance value in that index (as indicated in chiastic-security's answer).
Using an ArrayList/LinkedList the easiest way would probably be to create a class that represents both a char and its occurence, then add that object to the list.
<read char>
<search list for char>
<if list contains char>
<increment char's occurence>
<else>
<create a new char/occurence-object and add it to the list>