The Artima Developer Community
Sponsored Link

Java Answers Forum
Counting Different Characters

3 replies on 1 page. Most recent reply: Apr 21, 2002 4:31 AM by Natalie

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Natalie

Posts: 10
Nickname: natalie
Registered: Mar, 2002

Counting Different Characters Posted: Apr 16, 2002 9:40 AM
Reply to this message Reply
Advertisement
I posted a message earlier and I got a good response that was very helpful, but I have one more question.

I am writing a program that reads the number of sentences, words, and number of occurances of each alphabetical character taken from a .txt file, imported by the user. I figured out the words and sentences by using string tokenizers, but I'm not sure what to do about the chars.

Should I make two for loops? If so can you give me a simple example. I found a few but they are more complex than I need and I can't get my head around them. Or any other suggestions would be helpful. Sorry, I'm new at the whole programing thing.


Hiran

Posts: 41
Nickname: jclu
Registered: Mar, 2002

Re: Counting Different Characters Posted: Apr 17, 2002 12:53 PM
Reply to this message Reply
You could use the indexOf method in the String class or StringBuffer class (I'm not sure about StringTokenizer...haven't used it). If you haven't already done so, I suggest reading in the file and holding its contents in a StringBuffer. That way you don't have to keep the file open, and by using StringBuffer you aren't dealing with an immutable object. Anyway, have a variable that holds the index of the character (that is, the returned result of the indexOf method). Have a while loop that goes through until the above variable is -1 (which is what indexOf returns if there are no more occurences of the character). Lastly, have a counter variable that increases whenever the index variable is not -1. Something like this:
<java>
int index = -1;
int counter = 0;
//assuming that your StringBuffer variable is called
//fileContents.
index = fileContents.indexOf("c");
while (index != -1)
{
counter ++;
index = fileContents.indexOf("c");
}
</java>
You could also go through the whole StringBuffer variable and check each character (using the charAt method) and keep track of the number of characters that match. If you do it this way, you could use a StringBuffer (or String) variable, or you could read directly from the file one character at a time. The only problem with this method is that you then have to go through every character. With the indexOf method you'll probably have less iterations, and therefore your program will be faster. Hope this helps.
Hiran

> I posted a message earlier and I got a good response
> that was very helpful, but I have one more question.
>
> I am writing a program that reads the number of
> sentences, words, and number of occurances of each
> alphabetical character taken from a .txt file,
> imported by the user. I figured out the words and
> sentences by using string tokenizers, but I'm not
> sure what to do about the chars.
>
> Should I make two for loops? If so can you give me a
> simple example. I found a few but they are more
> complex than I need and I can't get my head around
> them. Or any other suggestions would be helpful.
> Sorry, I'm new at the whole programing thing.

Natalie

Posts: 10
Nickname: natalie
Registered: Mar, 2002

Re: Counting Different Characters Posted: Apr 18, 2002 2:32 AM
Reply to this message Reply
Thanks that helps a lot! :)

Natalie

Posts: 10
Nickname: natalie
Registered: Mar, 2002

Re: Counting Different Characters Posted: Apr 21, 2002 4:31 AM
Reply to this message Reply
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]

Flat View: This topic has 3 replies on 1 page
Topic: JTable - Save preferences Previous Topic   Next Topic Topic: CardLayout and JMenuBar

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use