-1

I want to count the number of each alphabetic character in this text file, unless the line begins with a ">" in which case it will simply write that line to the file. This is what I have, and it will not compile because it says "error: cannot find symbol" and points to the period in my for-statement line.length.

Why isn't this working??

String line;
while ((line = br.readLine () ) != null)
{
    if (line.startsWith( ">" ))
    {
        line += "\t";
        bw.write (line);
    }
    else
    { 
            int aCounter=0;
            int bCounter=0;
            int cCounter=0;
        for (int m=0; m < line.length; m++)
        {
            char letter = line.charAt(m);


            switch (letter)
            {
                case 'A':
                    aCounter++;
                    break;
                case 'B':
                    bCounter++;
                    break;
                case 'C':
                    cCounter++;
                    break;
            }
            }    
        bw.write( "A:" + aCounter + " B:" + bCounter + " C:" + cCounter);

}

file to be read sample:

this is a program that will count characters abcdabcdababab

wanted program output:

this is a program that will count characters a:5 b:5 c:2 d:2

  • line.length() should fix your problem. Also, as a good JAVA practice, you can use hashmap to keep track of each character's count. To determine if the char you have is alphabetical character, you can use Character.isLetter(string.charAt(m)). Refer to this entry for more detail: http://stackoverflow.com/questions/4047808/what-is-the-best-way-to-tell-if-a-character-is-a-letter-or-number-in-java-withou – Ensar Hatipoglu Dec 02 '14 at 20:50
  • Doing the above Hashmap idea is also an easy way to avoid creating 26 different variables. Creating that many variables is a bad idea, both from a coding perspective (more variables means more chances to mess up) and from a laziness perspective (lots of typing, or lots of copy/paste/change, which creates more coding errors) – Xynariz Dec 02 '14 at 21:08
  • Thanks for the help! It runs and compiles smoothly now, but the count of each letter is not printing correctly. It is printing like: A:0 B:0 A:0 A:0 B:1 A:0 B:0 – Devon Peterson Dec 02 '14 at 22:14

2 Answers2

4

It should be line.length() with the parameters denoting a method:

for (int m=0; m < line.length(); m++ )
M A
  • 71,713
  • 13
  • 134
  • 174
1

You must be confused with array's length property and string's length() method. The methods will have to be accessed with () (parenthesis) and the properties can be accessed by just names.

UPDATE: You need to initialize the counters above the for loop. They are resetting to 0 in each iteration of the loop.

int Acounter=0; int Bcounter=0; int Ccounter=0;

these should be above the for loop.Another suggestion is, in java variables has to start with a lower case letter (and classes should start with upper case letter). Ex: ACounter should be aCounter.

RP-
  • 5,827
  • 2
  • 27
  • 46
  • great suggestion! It compiles and runs now, but it prints the number of each letter incorrectly. It is something like A:0 A:0 B:1 A:0 A:0 A:0 C:1 – Devon Peterson Dec 02 '14 at 22:11
  • I had those variables initialized already, and I have changed all of the variables to lower-case as you have suggested. Good call. For some reason, I am getting the same answer. Thank you SO much for helping. I am having so much trouble with such an easy program. – Devon Peterson Dec 03 '14 at 01:24
  • @DevonPeterson Actually you need to bring `bw.write()` out of the for loop. It should be just after the for loop. It is like this.. You initialized counter to 0 and reading line and iterating the line's char and keep updating the counter and after you done with line (for loop) you need to print the counter. – RP- Dec 03 '14 at 01:58
  • Okay I have done so, and now it counts the correct number of characters but lists them several times like: a:3 b:2 a:2 a:1 b:1 and it should be like: a:6 b:3 – Devon Peterson Dec 03 '14 at 02:36
  • You want it to count for each line separately or all of the lines together? If so, you need to initialize the counters before the while loop and need to print them after the while. – RP- Dec 03 '14 at 02:39
  • Sorry, I had already done this but I forgot to edit it above. It is officially edited the same above as in my program. – Devon Peterson Dec 03 '14 at 03:22