Urgent! Java Assignment with loops in Arrays
I am relatively new to Java, and Arrays and using loops in arrays is proving a little trivial for me. I have the following assignment (most of which is done), I really need help in solving a certain chunk of code:
The purpose of this assignment is to familiarize you with arrays and lops in Java. In this
assignment you will have to implement a program that calculates and visualizes the character
count for all letters in a text file. Your program will have to do the following:
- Read the file into an array of chars. The program skeleton provided already does this.
- Go through every character in the array and calculate the number of occurrences of every letter
(you can assume that the text is written using the 26-letter English alphabet). All other
characters must be ignored. Upper- and lower-case forms of the same letter must be treated
the same - e.g. 'A' counts as an occurrence of 'a'. You must use an array of 26 ints to store the
counts, i.e., don't use separate variables like countA, countB, countC, etc.
- Once you have processed the entire array, display the histogram of character counts in one of
the two ways:
1. Count histogram, where the length of each bar - i.e. the number of characters in it - is the
calculated count for that character. For example, if there were 10 a's, 8 b's, and 13 c's, the
first three lines of your program's output should look like this:
2. Normalized histogram. A text screen is typically only 80 characters wide, but many text files
will have histogram bars more than 80 characters long, resulting in rather ugly output. To
avoid this, you will have to normalize the length of the histogram bars, so that the longest bar
is no more than, say, 40 characters long. You can do this using the following algorithm:
1. Find the letter with the largest count maxCount
2. If maxCount > 40, calculate the scaling factor as 40 / maxCount
3. To compute the normalized counts, multiply the count for every letter by this scaling
4. Display the histogram using the normalized counts
Two sample text files are provided: wonderland.txt (the full text for "Alice in Wonderland") and looking_glass.txt ("Through the Looking Glass"). Use these files to test the program.
The files are passed as command line arguments, e.g.:
java Assign2 wonderland.txt
You should probably create some other smaller text files to test your program's correctness. Also, read up on the char type in Java and how characters are represented (textbook/lecture/Teh Interwebz). Characters have integer codes and they are sequential. In other words, the code for 'b' is the code for 'a'+1; the code for 'z' is the code for 'a'+25. Use this idea and you will avoid writing a switch statement with 26 cases...
Attached are the two text files mentioned.
This is the code I have worked on so far, and the commented area shows where I need help
Through the Looking Glass:
public class Assign2
public static void main(String args)
int maxC = 0;
int counts = new int;
if (args.length != 1)
System.out.println("Invalid number of arguments");
System.out.printf("Usage: Assign2 file_name\n");
//If readFile cannot open the file, it exits the program
textArr = readFile(args);
//A bunch of for loops go here, with one nested for loop I am sure.
//Don't know what else to do. NEED HELP!!
public static char readFile(String fileName)
final int MAX_FILE = 1000000;
char tmpArr = new char[MAX_FILE];
char textArr = new char;
BufferedReader in = new BufferedReader(new FileReader(fileName));
int i = 0;
while ( (tmp = in.read()) != -1 )
tmpArr[i++] = (char)tmp;
textArr = new char[i];
System.arraycopy(tmpArr, 0, textArr, 0, i);
System.out.printf("read %d chars\n", i);
catch (Exception e)
System.out.println("Failed to open "+fileName+". Does the file exists?");
Alice in Wonderland: