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: Array and Loop?

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    26
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Array and Loop?

    Goal: To count the number of times each letter (a-z, both upper and lower case) appears in a file. Lower and upper case versions count as the same letter. So a's and A's both will be counted as a's. I am supposed to use an array

    So I have three problems.
    1. I'm not sure if I should do letters (a-z) for my array or numbers (0 - 25).
    2. Would a while loop be the best? Or a for loop?
    3. How do I combine the use of the scanner and the array in the loop?

    My code so far:

    /*
     * Count the number of times a letter appears in a text file
     * 
     * @author Kristen Watson
     * @version 11/12/2012
     * 
     */
     
    import java.util.Scanner;
    import java.io.File;
     
    public class LetterInventory{
        public final static String filename = "testFile.txt";
     
        public static void main(String[] args) {
            Scanner inputFile = null;
            try {
                inputFile = new Scanner(new File(filename));
            } catch (Exception e) {
                System.out.println("File could not be opened: " + filename);
                System.exit(0);
            }
     
            countOccurrences(inputFile);
            displayTable();
        }
     
     
        /*
         * constructor
         * inventory of letters with maximum of 26 different letters
         */
        public LetterInventory(){
            char []inventory = {'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'};  
     
        }
     
        /*
         * scanner takes information from the file and counts the letters 
         */
     
        public static void countOccurrences(Scanner file) {
     
             // file.next();
     
     
        }
     
     
     
        /*
         * output of the counted letters
         */
        public static void displayTable(){
            System.out.println("a:" );
            System.out.println("b:" );
            System.out.println("c:" );
            System.out.println("d:" );
            System.out.println("e:" );
            System.out.println("f:" );
            System.out.println("g:" );
            System.out.println("h:" );
            System.out.println("i:" );
            System.out.println("j:" );
            System.out.println("k:" );
            System.out.println("l:" );
            System.out.println("m:" );
            System.out.println("n:" );
            System.out.println("o:" );
            System.out.println("p:" );
            System.out.println("q:" );
            System.out.println("r:" );
            System.out.println("s:" );
            System.out.println("t:" );
            System.out.println("u:" );
            System.out.println("v:" );
            System.out.println("w:" );
            System.out.println("x:" );
            System.out.println("y:" );
            System.out.println("z:" );   
     
        }
     
     
     
        public static void resetInventory(){
     
     
        } 
    }



    Driver provided for the code

    *  It opens a test file and then uses a student written
     *  class called LetterInventory to count the number of
     *  times each letter occurs in the test file.
     * 
     * @author Clark Olson
     * @version 11/7/2012
     */
     
    import java.util.Scanner;
    import java.io.File;
     
    public class LetterCounter {
     
        public final static String filename = "testFile.txt";
     
        // Driver to test LetterInventory class
        public static void main(String[] args) {
            Scanner inputFile = null;
            try {
                inputFile = new Scanner(new File(filename));
            } catch (Exception e) {
                System.out.println("File could not be opened: " + filename);
                System.exit(0);
            }
     
            LetterInventory inventory = new LetterInventory();
            inventory.countOccurrences(inputFile);
            inventory.displayTable();
            inventory.resetInventory();
        }
     
    }


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Array and Loop?

    Here's the way that I see it:

    You need an array of 26 integers. Each element of the array will hold the count of a particular character 'a' through 'z'. You could call the array counters. The counters array is declared and initialized in the LetterInventory class.

    counters[0] will hold the count for the character 'a'
    counters[1] will hold the count for the character 'b'
    .
    .
    .
    counters[25] will hold the count for the character 'z'



    Then in the countOccurrences method of that class:

    Make a loop that reads lines from the given Scanner file.  Each line is read into a String.
    A conventient form of loop can be something like:
     
            while (file.hasNextLine()) {
                String line = file.nextLine();
                // Do something with the String
            }
     
    What is that "something that you do with the String?
     
    Well...
    Convert each String read by the Scanner nextLine() method to lower case and then do something like the following
     
    Make a loop that goes through all of the characters in the String.
     
    Inside the loop it goes something like this:
        Use the charAt() method to retrieve the current character
     
        Declare an integer variable named x and set x equal to the integer value of the current character minus 'a'.
        (That means that 'a' becomes 0, 'b' becomes 1, ... , 'z' becomes 25)
     
        If x is greater than or equal to zero and x is less than the number of elements in the counters array, then
          Increment counters[x]
        End if
       (That means that spaces and punctuation and everything other than a alphabetic characters will be ignored.)

    Finally...

    The displayTable method would print out the value of each of the counters in some kind of loop like
            for (int i = 0; i < counters.length; i++) {
                System.out.printf("%c: %5d\n", (char)(i + 'a'), counters[i]);
            }

    So a file containing the single line "The quick brown fox jumps over the lazy dog." would have the following output:
    a:     1
    b:     1
    c:     1
    d:     1
    e:     3
    f:     1
    g:     1
    h:     2
    i:     1
    j:     1
    k:     1
    l:     1
    m:     1
    n:     1
    o:     4
    p:     1
    q:     1
    r:     2
    s:     1
    t:     2
    u:     2
    v:     1
    w:     1
    x:     1
    y:     1
    z:     1



    Cheers!

    Z

  3. The Following User Says Thank You to Zaphod_b For This Useful Post:

    Kristenw17 (November 16th, 2012)

Similar Threads

  1. Array and Loop?
    By Kristenw17 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 13th, 2012, 04:58 AM
  2. frustrating loop/array issue
    By knightsb78 in forum Loops & Control Statements
    Replies: 6
    Last Post: August 11th, 2012, 05:47 PM
  3. Loop through a 2d array of objects
    By ssjg0ten5 in forum Loops & Control Statements
    Replies: 1
    Last Post: March 28th, 2012, 09:53 PM
  4. For loop in array
    By Mickeydus in forum Loops & Control Statements
    Replies: 2
    Last Post: March 26th, 2012, 02:37 PM
  5. [SOLVED] Array loop problem which returns the difference between the value with fixed value
    By uplink600 in forum Loops & Control Statements
    Replies: 5
    Last Post: May 15th, 2009, 04:31 AM