Solved

To count the no. of different letters in an input file -java-* urgent*

Posted on 2004-04-16
13
422 Views
Last Modified: 2008-02-01
Im struggling to write a program which can be used to count the frequency of different letters in a given input file i.e  "a"-20, "b"-10. ..i dont need to distuingish capital letters and small letters since i can convert them into one of them. I wanted to use arrays . All i wanted was help on the writing the code for counting the letters and dispaying the results at the end of it...please help..coz i really need to do it as soon as i can
0
Comment
Question by:kenyapeke
  • 4
  • 3
  • 3
  • +1
13 Comments
 
LVL 100

Expert Comment

by:mlmcc
ID: 10844143
This sounds suspiciously like a homework problem.  If so, the experts are not allowed to write your program.  The experts can help you with specific questions or specific parts of the code but you need to provide the code you have already written.  We can then comment on it and point out where the errors are.

mlmcc
0
 
LVL 3

Expert Comment

by:itbeme
ID: 10844218
As you study Java go through Sun's tutorials, they are good.
Here's one on file reading
http://java.sun.com/docs/books/tutorial/essential/io/filestreams.html
I think once you can accomplish that process. The rest of the program should fall into place.
0
 
LVL 2

Expert Comment

by:RNMcLean
ID: 10847338
You should be able to organise your thoughts. From the statement of the problem, the task is:
 first, to break the input into a stream of individual characters,
 second, count the various characters.
 then presumably, make some report of the counts when all input has been read.
0
 

Author Comment

by:kenyapeke
ID: 10848846
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
 
LVL 100

Expert Comment

by:mlmcc
ID: 10851204
Good try.  I don't know Java but  here is the basic algorithm.

Declare your array for counting
Open the file for input
While Not EOF
  read a character
  Determine the character index 0-25
  increment appropriate character counter
next character

Print out the count

mlmcc
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:kenyapeke
ID: 10851581
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 100

Expert Comment

by:mlmcc
ID: 10851813
Can you show the new code?

mlmcc
0
 

Author Comment

by:kenyapeke
ID: 10851853
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 = fis.read() ) != -1 )
     {

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

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


     fr.close();
     }
   }
  }
0
 
LVL 3

Expert Comment

by:itbeme
ID: 10851889
One of your problems is:
while ((inString = buffer.readLine()) !=null) {
       String upperCase = inString.toUpperCase();
       System.out.println(upperCase);
     }
Brings the file pointer to the end of the file.  Therefore when you read from the file again, you're already at the end.
0
 

Author Comment

by:kenyapeke
ID: 10851967
So how could i solve the problem..pliz illustrate..and if possible show the solution..Thanx..
0
 
LVL 3

Accepted Solution

by:
itbeme earned 125 total points
ID: 10854751
There are different ways you can do it;

you could write the input file to a temp file in uppercase, then read from that file.

You could have two test arrays, one to hold lowercase chars and one to hold uppercase, then test your chars from the file against both.

You could do something like this
http://oldlook.experts-exchange.com:8080/Programming/Programming_Languages/Java/Q_20809573.html

Read each line of the file out into a String.  Convert string to upper case.  Put the String into a character array test those elements against your test array.

Is to read each line of the file out into a String.  Use substring to bring out one character length strings at a time and test those against your capital letter test array. Either convert the string to uppercase, or use strings equalsIgnoreCase()  to compare the two strings.  Make sure you change your char array into a string array.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
This is about my first experience with programming Arduino.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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 …

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now