Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-04-16
13
Medium Priority
?
434 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +1
13 Comments
 
LVL 101

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 101

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 101

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 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

A short article about problems I had with the new location API and permissions in Marshmallow
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
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 …
Starting up a Project

618 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