Solved

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

Posted on 2004-04-16
13
426 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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
 

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

This article will show, step by step, how to integrate R code into a R Sweave document
A short article about a problem I had getting the GPS LocationListener working.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

803 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