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?

    This is my code so far:
    /*
     * Count the number of times a letter 
     * 
     * @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(){
     
     
        } 
    }


    And this is the drive that was provided:
    *  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
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Array and Loop?

    1. Arrays are indexed using integer data types. Think about what each element of the array is suppose to represent.

    2. Think about how you want to structure your control flow. For loops work best when you want an iterative-type process (e.g. for each character in a string, do something). While loops are more general purpose (e.g. while there are still characters left to process, do something). You may find either solution works.

    3. First figure out what a Scanner can do. Then figure out how you want to structure your control flow (related to #2). Check The Scanner Javadoc. Hint: take a look at the next() method and the nextLine() method.

Similar Threads

  1. frustrating loop/array issue
    By knightsb78 in forum Loops & Control Statements
    Replies: 6
    Last Post: August 11th, 2012, 05:47 PM
  2. Loop through a 2d array of objects
    By ssjg0ten5 in forum Loops & Control Statements
    Replies: 1
    Last Post: March 28th, 2012, 09:53 PM
  3. For loop in array
    By Mickeydus in forum Loops & Control Statements
    Replies: 2
    Last Post: March 26th, 2012, 02:37 PM
  4. Array Lists filling for loop
    By rob17 in forum Collections and Generics
    Replies: 9
    Last Post: March 22nd, 2012, 08:28 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