aspose file tools*
The moose likes Beginning Java and the fly likes File Letter Counter help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "File Letter Counter help" Watch "File Letter Counter help" New topic
Author

File Letter Counter help

Joel Allison
Greenhorn

Joined: Nov 17, 2009
Posts: 9
Not sure what my problem is here, trying to put this together with a very small knowledge of what I'm actually doing :-\ Not even sure if I'm posting in the right format.

Anyways, I'm trying to make a program that reads a filename and a character from the user, then returns the amount of times the letter appears inside that file. I have to do this with a function, the following code is as good my knowledge will get it, but it keeps saying the following errors. Any help would be greatly appreciated.


~ $ javac Prog7.java
Prog7.java:46: cannot find symbol
symbol : class IOException
location: class Prog7
static int frequency(char ch, String fname) throws IOException
^
Prog7.java:51: cannot find symbol
symbol : class FileInputStream
location: class Prog7
FileInputStream f=new FileInputStream(new file(fname));
^
Prog7.java:51: cannot find symbol
symbol : class FileInputStream
location: class Prog7
FileInputStream f=new FileInputStream(new file(fname));
^
Prog7.java:51: cannot find symbol
symbol : class file
location: class Prog7
FileInputStream f=new FileInputStream(new file(fname));





Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

The compiler is complaining that it cannot understand what you mean by IOException and FileInputStream classes. The solution is to tell it where this information is located.
Hint: When you order goods from outside of your country that transaction type is called import.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Somnath Mallick
Ranch Hand

Joined: Mar 04, 2009
Posts: 477
All I can say is you have to break down the strings returned from the file into individual character and then check with the alphabet and increment the counter.

Better use String args[] to take the input, which would make the code simpler! Also you may use some System.out.println's to print some comments on the console!!
Joel Allison
Greenhorn

Joined: Nov 17, 2009
Posts: 9
Thanks for the advice, I think I'm about done but now I'm getting the error below, but when I try to add ";" in there I get the same error if it's after "throwsIOException" or if I add it before it says something about an identifier is expected. I've tried looking up my problem but I just can't get this to work.

~ $ javac Prog7.java
Prog7.java:47: ';' expected
static int frequency(char ch, String fname) throwsIOException


PrasannaKumar Sathiyanantham
Ranch Hand

Joined: Nov 12, 2009
Posts: 110
your static int frequency() method has been declared to throw ioexception.....

hence when you call this method it must be surrounded by the try catch block
like this
try{
frequency();
}
catch(IOException e )
{}

try this it must work

also its a good programming practice to import only the packages or classes that you use in the program....don't import everything kkkkkk


To err is human,
To forgive is not company policy
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

Change to
Notice the whitespace between throws and IOException
Joel Allison
Greenhorn

Joined: Nov 17, 2009
Posts: 9
Ahh thanks, but I've ran into yet another error ugh :-(
I just can't seem to understand how to use this IOException.

~ $ javac Prog7.java
Prog7.java:39: unreported exception java.io.IOException; must be caught or declared to be thrown
count=frequency (ch, fname);
Somnath Mallick
Ranch Hand

Joined: Mar 04, 2009
Posts: 477
PrasannaKumar Sathiyanantham wrote:your static int frequency() method has been declared to throw ioexception.....

hence when you call this method it must be surrounded by the try catch block
like this
try{
frequency();
}
catch(IOException e )
{}

try this it must work

also its a good programming practice to import only the packages or classes that you use in the program....don't import everything kkkkkk


Did you try this?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

Recommended reading http://java.sun.com/docs/books/tutorial/essential/exceptions/

In your code, you have declared that your method frequency might thrown an IO Exception.
In another part of your code, you are invoking this method.
What the compiler is telling you is that in case the frequency method does throw an IO exception, there is nobody (no code) to handle it.
More details on how to handle exceptions are in the link I have provided.
Somnath Mallick
Ranch Hand

Joined: Mar 04, 2009
Posts: 477
Better get rid of the throws and surround the code with a try catch!
Joel Allison
Greenhorn

Joined: Nov 17, 2009
Posts: 9
I can get it to compile, but when I try to set count equal to the return value of the function called, it says it is an illegal start of an expression.

So how can I get count to equal the returned value by the function?
Somnath Mallick
Ranch Hand

Joined: Mar 04, 2009
Posts: 477
Could you post your code as it is now?
Joel Allison
Greenhorn

Joined: Nov 17, 2009
Posts: 9
This is my current code and it will compile, however I need to take the value returned from the function and print it in the println. To do that I was trying to set count equal to the value returned, but when I try to write this it said the error I posted above.

count = try{
frequency (ch, fname);
}
catch(IOException e )
{}


PrasannaKumar Sathiyanantham
Ranch Hand

Joined: Nov 12, 2009
Posts: 110
see in line 20..

The static frequency function returns count value...

but in the main method there is nothing to capture this value and hence the error

it must be like this
try{
count=frequency();
}
catch(IOException e){}

I Strongly suggest you read java2 complete reference by herbert schildt or head first java for core java understanding...(i chose complete reference)

it makes you think where as HFJ spoon feeds you like feeding a child

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: File Letter Counter help