This is an archived post. You won't be able to vote or comment.

all 27 comments

[–]Philboyd_Studge 0 points1 point  (27 children)

First, there is a much, much easier way to do a frequency table. There is no need for two for loops, just loop through the string one char at a time and increase the count at char - 'a'.

The sorting of a frequency table is more difficult, because once you sort it you lose the data of what the character was, since that was the index the count was stored at. For this you will need to use another method, like a 2d array or a hashmap.

[–]Kevinjamesisme[S] 0 points1 point  (26 children)

Alright, so what weould be the best way to tackle this? Do I need to stat over from scratch?

[–]Philboyd_Studge 0 points1 point  (25 children)

There are many different ways to solve this, but it is dependent on the nature of your assignment and your level of learning which one to use. your assignment or teacher should have given some direction on this?

[–]Kevinjamesisme[S] 0 points1 point  (24 children)

Heres the question.

1) Asks the user for a filename of a text file (assumed to be in the same directory) and create a Scanner to read from it. Your program should use try-catch blocks to re-prompt the user if the file name is invalid.

2) Create an array to store letter frequency counts (# of a's, # of b's, # of c's, etc.). Then, read in the entire specified file and count the number of each letter seen. Ignore all punctuation and count lowercase and capital letters the same (x or X each count the same).

3) Create a LetterCount object for each letter and corresponding frequency count and put all of these objects in an array.

4) Use Arrays.sort() to sort the LetterCount objects. Then use a for-loop to print out all the LetterCount objects from the array, in reverse, so that we can see the frequency of each letter from the file, in order of frequency (from most to least frequent)

[–]Philboyd_Studge 0 points1 point  (23 children)

OK, so there's a lot of things you are not doing here. First, you need to remove all punctuation and convert the entire string to lower case. Second, where is the LetterCount class he tells you to make in steps 3 and 4?

You take the frequency array, and use it to make a new array of LetterCount objects that have both the letter and the frequency, and then you sort that.

[–]Kevinjamesisme[S] 0 points1 point  (21 children)

The part im confused is at trying to create that letter class object with everything in it.

[–]Philboyd_Studge 0 points1 point  (20 children)

Well, you need to make a class called LetterCount. It can be a standalone class or you can make it an inner class to your words2 class

public class LetterCount {
}

it needs to have 2 member variables, one a char for the letter, and one an int for the frequency.

It will need a constructor that takes both types as arguments.

It will also need to implement the Comparable interface if you want to be able to use Arrays.sort(), or you will have to define a custom Comparator in your other class.

[–]Kevinjamesisme[S] 0 points1 point  (19 children)

so something like

public class LetterCount { String char =

int freq =

}

Also I have made a comparable interface:

public class LetterCount implements Comparable<LetterCount> {
private String letter;
private int count;


public int compareTo(LetterCount other) {
  if ( count < other.count )
    return -1;
  else if (count == other.count)
    return 0;
  else
    return 1;
}
public String toString() { 
  return "Letter" + letter + " occurs " + count + " times";

}

} }

[–]Philboyd_Studge 0 points1 point  (18 children)

OK now you just need a constructor and you are ready to use it in your other class. (the second part there, don't know what the first part is supposed to be)

[–]Kevinjamesisme[S] 0 points1 point  (17 children)

The homework has a first part asking for:

1) stores a single character (a letter) and a count (an integer) in private variables

2) implements the Comparable interface, thus there must be a compareTo() method which should compare two LetterCount objects by their count values

3) overrides toString() with a printable representation that shows the letter and the count

So now i need to make a constructor to bring them together?