Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Need help with program please: getting symbol error

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Need help with program please: getting symbol error

    I have an assignment in my intro level programming class to write a program to count the number of times a character occurs in a file. My professor showed us how to write this program in class but I continue to get a missing symbol error on my FileInputStream line. Could someone please tell me how to fix this? Ignore my awful formatting in the program and realize that this is how my professor wants us to write this program. So try to keep it in this mindset. Appreciate the help! Here is my program as of now.
    import java.util.*;
     
    public class Prog7
    {
      public static void main(String[] args)
    {
      Scanner keybd = new Scanner(System.in);
     
    String filename;
    char ch;
    int count;
     
    //Prompt the user for filename.
     
    filename = keybd.next();
     
    count = frequency(ch,filename);
     
    System.out.println("In the file " + filename + ", the character " + ch + " appears " + count + " times.");
    }
     
    //Given a character ch and a filename.
    //Prints the number of times the character appears
    //in the file. Returns the count.
     
     
    public static int frequency(char ch, String fname)
    {
    FileInputStream f = new FileInputStream(newFile(fname));
     
    int count;
    char nextChar;
     
    count=0;
    nextChar=f.read();
     
    while(nextChar != -1)
    {if(nextChar==ch)
      count++;
     
      nextChar=f.read();
    }
    f.close();
     
    return count;
    }
    }
    Last edited by blinkzz; November 17th, 2009 at 09:27 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Need help with program please: getting symbol error

    Java will force you to initialize all variables before they can be used. I suspect you'll probably want some way that this variable can be changed dynamically by the user, but that doesn't necessarily have to be the case.

    char ch = 'a'; // example

    Also, the FileInputStream constructor can possibly throw a FileNotFoundException which must be caught, and the next() method throws a IOException which must be caught. The easiest way to get around this is to make both the frequency and the main methods have declared that they throw an IOException (since FileNotFoundException is a child of IOException)

    public static void main(String[] args) throws IOException

    I'm guessing this is a typo, but you need a space here:
    // your code
    FileInputStream f = new FileInputStream(newFile(fname));
    // correct code
    FileInputStream f = new FileInputStream([b]new File[/b](fname));

    Also, the read method technically gives back a byte, which Java tries to cast up to an int. To get it to compile, you must put an explicit cast to a char

    nextChar = (int) f.read();

Similar Threads

  1. Replies: 2
    Last Post: November 3rd, 2009, 06:28 AM
  2. cannot find symbol variable radius?
    By noobish in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 4th, 2009, 11:29 AM
  3. Error of data types and type casting in java program
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: September 2nd, 2009, 11:22 AM
  4. Error while creating Gym member database through Java programming
    By parvez07 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 26th, 2009, 03:17 AM
  5. Redirect error and output stream using java program
    By leenabora in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: June 16th, 2009, 05:12 AM