Natalie
Posts: 10
Nickname: natalie
Registered: Mar, 2002
|
|
Re: Counting Different Characters
|
Posted: Apr 21, 2002 4:31 AM
|
|
Just to answer my own question. I used two arrays. Here's the code:
char [] letters = {'a','b','c','d','e','f','g','h','i','j', 'k','l','m','n','o','p','q','r','s','t', 'u','v','w','x','y','z'}; int frequency[] = new int[26]; int x; //frequency counter int y; //alphabet int a; //length of string File inFile = chooser.getSelectedFile(); FileReader reader = new FileReader(inFile); BufferedReader in = new BufferedReader(reader); String line; String output = "Letter\tFrequency\n"; while ((line = in.readLine()) != null) { String lineLower = line.toLowerCase(); for (a = 0; a < lineLower.length(); a++){ for(y = 0; y < 26; y++) if(lineLower.charAt(a) == letters[y]) ++frequency[y]; } } for(x = 0; x < frequency.length; x++) { output += "'"+letters[x]+"' : \t" +frequency[x]+ "\n"; } JTextArea outputArea = new JTextArea(); outputArea.setText(output); outputArea.setEditable(false); JOptionPane.showMessageDialog(null, outputArea, "All Letter Frequencies", JOptionPane.INFORMATION_MESSAGE); }//end choice [2]
|
|