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) is used in a file. The code is workin (atleast it appears to be), but I have a couple issues.

    1. I am wanting the array to be initialized in the constructor but when I do that the other methods can't access is. Is there a way to make that work?
    2. I am not sure how to reset the counter at the end in the resetInventory method


    /*
     * 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 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();
        }
     
        int[]counters = new int[26];
        char current;
        int value;
     
        /*
         * constructor
         * inventory of letters with maximum of 26 different letters
         */
        public LetterInventory(){
     
     
        }
     
        /*
         * scanner takes information from the file and counts the letters 
         */
        public void countOccurrences(Scanner file) {
            while(file.hasNextLine()){
                //take information one line at a time
                String line = file.nextLine();
     
                //convert all letters in the string to lower case
                line.toLowerCase();
     
                for(int i=0; i < line.length(); i++){
                    current = line.charAt(i);
                    if (current >= 'a' && current <= 'z'){ //check that the char is a letter
                        value = (int)(current - 'a');
                        counters[value]++;
                    }
     
                }
     
            }
        }
     
     
        /*
         * output of the counted letters
         */
        public void displayTable(){
           for (int i = 0; i < counters.length; i++){
               current = (char)(i + 'a');
               System.out.println(current + ":" + counters[i]);
           }
     
        }
     
        public void resetInventory(){
     
     
        } 
    }


    Gracias to anyone who can explain to me the error of my ways


  2. #2
    Junior Member
    Join Date
    Mar 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array and loop

    There are a couple of minor issues.

    1. You main method is defined incorrectly, it should be
    public static void main(String [] args)

    2. Create an instance of the LetterInventory object in the main method and call methods on todhe object. You can initialize the counters in the constructor as all methods of the object will be able to see it.

Similar Threads

  1. Array and Loop?
    By Kristenw17 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 13th, 2012, 02:23 PM
  2. Array and Loop?
    By Kristenw17 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 13th, 2012, 04:58 AM
  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