x
Solved

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

Posted on 2004-04-16
Medium Priority
437 Views
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
Question by:kenyapeke
• 4
• 3
• 3
• +1

LVL 101

Expert Comment

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

ID: 10844218
As you study Java go through Sun's tutorials, they are good.
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

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

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 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'};

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 101

Expert Comment

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

Open the file for input
While Not EOF
Determine the character index 0-25
increment appropriate character counter
next character

Print out the count

mlmcc
0

Author Comment

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)

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

0

LVL 101

Expert Comment

ID: 10851813
Can you show the new code?

mlmcc
0

Author Comment

ID: 10851853
import java.io.*;

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'};

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

ID: 10851889
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

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

LVL 3

Accepted Solution

itbeme earned 500 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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.