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.
See Question&Answers more detail:os