Alright. So this is just one part of my multiple part project.

I need to try and get the frequency of how many times each word of 'X' amount of letters shows up.

I am pretty bad at using arrays, but I realize this is the best way to do it and the only way with what I've learned so far.


Here is what I have so far. It prints out like everything. Every word from the file line by line and also a long list of all 15 digits and for each line one of the digits goes up.

How can I get the information from only the last line that has the final count?

private int wordCount;
	private int lineCount;
	private int charCount;
	private double avgWordLength;
	private final int charactNumbers = 26;
	private int[] high = new int[charactNumbers];
	private int alphabet;
	private int otherChars = 0;
	private final int LIMIT = 15;
	int[] wordLength = new int[LIMIT];
	int len;

public void wordFreq(String filename)
	{
	File inFile = new File(filename);
	    try {     Scanner file = new Scanner(inFile);
	    
	    while ((file.hasNext()))
		{
		String s = file.next();
		System.out.println (s);
		int wat = s.length();
		wordLength[wat]++;
		if (wat != 0)
		{
		for (int dogs : wordLength)
		System.out.print (dogs + " ");
		}
		}
	    
	    } catch (IOException e) {
	    }		
	}

when I take out the 'unnecessary println (s)' I get an output like this:

0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 2 0 0 1 0 0 0 0 0 0 0 0 0 0 1 2 0 0 1 0 0 0 0 0 0 0 0 1 0 1 2 0 0 1 0 0 0 0 0 0 0 1 1 0 1 2 0 0 1 0 0 0 0 0 0 0 1 1 0 2 2 0 0 1 0 0 0 0 0 0 0 1 1 0 3 2 0 0 1 0 0 0 0 0 0 0 1 1 1 3 2 0 0 1 0 0 0 0 0 0 0 1 1 2 3 2 0 0 1 0 0 0 0 0 0 0 1 1 2 3 2 1 0 1 0 0 0 0 0 0 0 1 1 2 3 2 1 0 1 0 0 1 0 0 0 0 1 1 2 3 2 1 0 1 0 1 1 0 0 0 0 1 2 2 3 2 1 0 1 0 1 1 0 0 0 0 1 2 2 3 2 2 0 1 0 1 1 0 0 0 0 1 3 2 3 2 2 0 1 0 1 1 0 0 0 0 1 3 3 3 2 2 0 1 0 1 1 0 0 0 0 1 4 3 3 2 2 0 1 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1 0 2 0 0 1 11 12 8 9 4 2 2 0 1 1 0 2 0 0 1 11 12 8 9 4 2 2 0 2 1 0 2 0 0 1 11 13 8 9 4 2 2 0 2 1 0 2 0 0 1 11 14 8 9 4 2 2 0 2 1 0 2 0 0 1 11 14 8 9 4 2 3 0 2 1 0 2 0 0 1 11 15 8 9 4 2 3 0 2 1 0 2 0 0 1 11 16 8 9 4 2 3 0 2 1 0 2 0 0 1 12 16 8 9 4 2 3 0 2 1 0 2 0 0 1 12 17 8 9 4 2 3 0 2 1 0 2 0 0 1 12 17 8 9 4 3 3 0 2 1 0 2 0 0 1 12 17 8 9 5 3 3 0 2 1 0 2 0 0 1 13 17 8 9 5 3 3 0 2 1 0 2 0 0 2 13 17 8 9 5 3 3 0 2 1 0 2 0 0 2 13 17 9 9 5 3 3 0 2 1 0 2 0 0 2 14 17 9 9 5 3 3 0 2 1 0 2 0 0 2 15 17 9 9 5 3 3 0 2 1 0 2 0 0 2 15 17 9 10 5 3 3 0 2 1 0 2 0 0 2 15 18 9 10 5 3 3 0 2 1 0 2 0 0 2 15 19 9 10 5 3 3 0 2 1 0 2 0 0 2 15 19 9 10 5 3 3 0 3 1 0 2 0 0 2 16 19 9 10 5 3 3 0 3 1 0 2 0 0 2 16 20 9 10 5 3 3 0 3 1 0 2 0 0 2 16 20 9 10 6 3 3 0 3 1 0 2 0 0 2 16 21 9 10 6 3 3 0 3 1 0 2 0 0 2 17 21 9 10 6 3 3 0 3 1 0 2 0 0 2 17 22 9 10 6 3 3 0 3 1 0 2 0 0 2 17 22 10 10 6 3 3 0 3 1 0 2 0 0 2 17 22 11 10 6 3 3 0 3 1 0 2 0 0 2 17 22 11 10 6 3 4 0 3 1 0 2 0 0 2 17 22 11 10 6 3 5 0 3 1 0 2 0 0 2 17 23 11 10 6 3 5 0 3 1 0 2 0 0 2 17 23 12 10 6 3 5 0 3 1 0 2 0 0 2 17 23 12 11 6 3 5 0 3 1 0 2 0

Hey, quick question. Do the numbers you posted represent the length of each word in the file? If they do, why not store them in an array rather than printing them to the screen? If you do that then computing the frequency of x letter words should be straight forward.

double count = 0, frequency;

for(int i = 0; i < array.length; i++) 
{
  if(array[i] == /*which ever number you decide on*/) 
  count++;
}

frequency = count/array.length;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.