# Array and Loop?

• November 12th, 2012, 11:56 PM
Kristenw17
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:
Code :

```/* * 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:
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(); }   }```
• November 13th, 2012, 03:58 AM
helloworld922
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.