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: count how frequently each character occurs

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default count how frequently each character occurs

    I have the code as shown below but i dont know how to count how frequently each character will occur when the user types in their file name. Any help will do
    // Written by A.Person, January 2010
    // Answer to Exercises 1 example question 2
    class Example1_2
    {
    private void displayFileContent(String filename)
    {
    FileInput fileIn = new FileInput(filename);
    while (fileIn.hasNextLine())
    {
    String s = fileIn.nextLine();
    System.out.println(s);
    System.out.println(s.length());
    }
    fileIn.close(); // Always close a file after it has been used.
    }
    private String getFileName()
    {
    Input in = new Input();
    System.out.print("Enter filename: ");
    String filename = in.nextLine();
    return filename;
    }
    public void showFile()
    {
    String filename = getFileName();
    displayFileContent(filename);
    // Could have written displayFileContent(getFileName());
    }
    public static void main(String[] args)
    {
    new Example1_2().showFile();
    }
    }
    Last edited by Json; February 19th, 2010 at 04:23 AM. Reason: Please use code tags.


  2. #2
    Member
    Join Date
    Feb 2010
    Location
    Auburn, AL
    Posts
    31
    Thanks
    0
    Thanked 8 Times in 8 Posts

    Default Re: count how frequently each character occurs

    I really don't quite know what you're asking. Do you have a problem with the code you have provided, or do you want to write new code to count the frequency with which each ASCII character appears? If it is the latter, just read the file one line at a time, and then step through each line one character at a time, and have some mapping from characters to indices in a frequency array whose elements will be incremented whenever the corresponding character is encountered.
    Let me know what you think of my website:
    http://www.auburn.edu/~carpept
    Comments or suggestions are appreciated!

Similar Threads

  1. select count(*)
    By jacinto in forum JDBC & Databases
    Replies: 4
    Last Post: March 2nd, 2010, 10:30 PM
  2. How can i add a new count to this source code ?
    By mm2236 in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: January 30th, 2010, 10:21 PM
  3. The character '' is an invalid XML character exception getting
    By sumanta in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 9th, 2010, 12:13 PM
  4. count components inside a JPanel
    By dewboy3d in forum AWT / Java Swing
    Replies: 1
    Last Post: August 2nd, 2009, 03:17 PM
  5. Creating A Count Matrix In Java
    By statsman5 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: July 14th, 2009, 04:40 PM