Solved

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

Posted on 2004-04-17
7
6,010 Views
Last Modified: 2007-12-19
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();
     }
   }
  }
0
Comment
Question by:chataholic4real
  • 3
  • 3
7 Comments
 
LVL 1

Expert Comment

by:BRPXQZME
ID: 10851265
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.
0
 

Author Comment

by:chataholic4real
ID: 10851578
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

0
 
LVL 1

Accepted Solution

by:
KartikShah earned 100 total points
ID: 10859088
Hi,

I tried your code, I have made a few modification to it.

The reason you got that exception, because, you were trying to read from the stream, which has alread reached the end of file.  You will need reopen the stream, else you can try, the modification below :)

-----------------------Start---------------------------

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("c:/kartik shah/tp/experts exchange/watu.txt");
      BufferedReader buffer = new BufferedReader(fr);
     
      String inString;
      /**
        * Creating a string buffer to store the data read, to avoid multiple reads. :)
       **/
      StringBuffer completeText = new StringBuffer();
     
      while ((inString = buffer.readLine()) !=null)
      {
        String upperCase = inString.toUpperCase();
        System.out.println(upperCase);
        completeText.append(upperCase);
      }
      /**
       * Since I am using the string buffer, next char is not required
       **/
      //int nextChar;
      char ch;
      int count=0;
      for (int i = 0; i < 26; i++)
      {
        count = 0;
       
        /**
         * Replacing while with a for loop, since using a StringBuffer
         **/
        //while ( (nextChar = inString.read() ) != -1 ) //***problem with this line**
        for (int j = 0; j<completeText.length(); j++)
        {
          ch = completeText.charAt(j) ;
          if( ch== capital[i])
          {
            count++;
          }
        }
        System.out.print("  " + capital[i]);
        System.out.println("          " + count);
        fr.close();
      }
  }
}

-------------------------End-------------------------------

Hope is helps you.

Kartik
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Author Comment

by:chataholic4real
ID: 10859278
I want to say thank you again Kartik..it works..i really appreciate..so much...
Thanx a million..
0
 
LVL 1

Expert Comment

by:KartikShah
ID: 10859893
Welcome anytime ... :)
0
 

Author Comment

by:chataholic4real
ID: 10873762
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
0
 
LVL 1

Expert Comment

by:KartikShah
ID: 10890217
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
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
This code tracks birthdays 3 82
Definitions and default visual studio colors 5 70
Difference between List and Array in Python 5 96
Programming Codes 2 22
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This article will show, step by step, how to integrate R code into a R Sweave document
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question