• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

File Letter Counter help

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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));





 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 483
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Change to
Notice the whitespace between throws and IOException
 
Joel Allison
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 483
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 483
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Better get rid of the throws and surround the code with a try catch!
 
Joel Allison
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 483
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you post your code as it is now?
 
Joel Allison
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic