Solved

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

Posted on 2004-04-16
13
424 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ClickOnce Install - Shortcut Question 3 73
Installed softare without GUID 6 66
Hide vba in gp 7 83
T-SQL:  Sigh---Boy, this is fun.... 12 34
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This is an explanation of a simple data model to help parse a JSON feed
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 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…

867 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

18 Experts available now in Live!

Get 1:1 Help Now