Link to home
Start Free TrialLog in
Avatar of chataholic4real
chataholic4real

asked on

Using array to count letters in a particular text file..(java)

This program is used in counting letters in particular text.It counts the frequency of each letter e.g z= 30, c=20. and prints the results.I managed to work out part of it. the main problem came in when counting the letters using arrays.. while i was trying to compile it..there was a problem with line i have highlighted(****). could any of u assist with this. and maybe if there is another problem..jus point out or assist..

import java.io.*;

public class BufferedReaderTest {

  public static void main(String args[]) throws Exception {
    char[] capital = { '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'};

    FileReader fr = new FileReader("watu.txt");
    BufferedReader buffer = new BufferedReader(fr);
   
    String inString;

    while ((inString = buffer.readLine()) !=null) {
       String upperCase = inString.toUpperCase();
       System.out.println(upperCase);
     }
       int nextChar;
               char ch;
               int count=0;
            for (int i = 0; i < 26; i++)
            {


     while ( (nextChar = inString.read() ) != -1 ) ***problem with this line**
     {

     ch = (char) nextChar;
     if( ch== capital[i])
     {
      count++;
                }

     }
       System.out.print("  " + capital[i]);
       System.out.println("          " + count);


     fr.close();
     }
   }
  }
Avatar of BRPXQZME
BRPXQZME

I don't think String has a read() method.

However, BufferedReader does.  As a matter of fact, FileReader does, too.  I admittedly don't know squat about Java, but you should try looking into using those, I think.  Also, you may find the Java documentation at http://java.sun.com/j2se/1.4.2/docs/api/index.html extremely helpful.
Avatar of chataholic4real

ASKER

i have rectified that line..(with an error)
its now..while ( (nextChar =fis.read() ) != -1 )
 The program compiles properly but..it gives
A=0
then it gives the following error message.

Exception in thread "main" java.io.IOException: Stream closed
        at sun.nio.cs.StreamDecoder.ensureOpen(StreamDecoder.java:37)
        at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:152)
        at sun.nio.cs.StreamDecoder.read0(StreamDecoder.java:131)
        at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:117)
        at java.io.InputStreamReader.read(InputStreamReader.java:151)
        at BufferedReaderTest.main(BufferedReaderTest.java:25)

i really dont know..where the problem is guyz..could u help

ASKER CERTIFIED SOLUTION
Avatar of KartikShah
KartikShah

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I want to say thank you again Kartik..it works..i really appreciate..so much...
Thanx a million..
Welcome anytime ... :)
is there a way i could modify this code so that instead of printing on the screen..i could print the results on a file...and also normalise the values i.e if a= 12, b=10, c=13...i could divide all the values by 13. so as to be able to plot a histo gram
Yes, you can do that easily. Al you will need to do , is open a filewriter object .. and write the data to that object. :-) The the fun of OOPs .. do more with less :D