Skip to main content
Lesson 19 - Single Dimension Arrays
Lesson MenuPreviousNext
  
Applications of Arrays page 6 of 11

  1. Suppose we have a text file votes.txt of integer data containing all the votes cast in an election. This election happened to have three candidates and the values in the integer file are 1, 2, or 3, each corresponding to one of the three candidates.

    Program 19-2

    import chn.util.*;
    
    public class Votes
    {
      public static void main (String[] args)
      {
        FileInput inFile = new FileInput("votes.txt");
        
        int vote, total = 0, loop;
       
        // sized to 4 boxes, initialized to 0's
        int[] data = new int[4];
    
        vote = inFile.readInt();
        while (inFile.hasMoreTokens())
        {
          data[vote]++;
          total++;
          vote = inFile.readInt();
        }
        System.out.println("Total # of votes = " + total);
        for (loop = 1; loop <= 3; loop++)
          System.out.println("Votes for #" + loop +
                             " = " + data[loop]);
    
      }
    }
    1. The array data consists of four cells, each holding an integer value. The first cell, data[0], is allocated but not used in this problem. After processing the entire file, the variable data[n] contains the number of votes for candidate n. We could have stored the information for candidate 1 in position 0, candidate 2 in position 1, and so forth, but the code is easier to follow if we can use a direct correspondence.

    2. The value of vote is used to increment the appropriate cell of the array by +1.

  2. A second example counts the occurrence of each alphabet letter in a text file.

    Program 19-3

    import chn.util.*;
    
    public class CountLetters
    {
      public static void main (String[] args)
      {
        FileInput inFile = new FileInput("sample.txt");
    
        int[] letters = new int[27]; // use positions 1..26
                                     //  to count letters
        int total = 0;
        char ch;
    
        while (inFile.hasMoreLines())
        {
          String line = inFile.readLine().toLowerCase();
          for (int index = 0; index < line.length(); index++)
          {
            ch = line.charAt(index); 
         // line.charAt is from chn.util. It extracts the entry.
    
            if ('a' <= ch && ch <= 'z')  // if we have a letter...
            {
              letters[ch - 96]++;  // if ch == 'a', 97-96 = 1, etc.
              total++;
            }
          }
        }
        System.out.println("Count letters");
        System.out.println();
        ch = 'a';
        for (int loop = 1; loop <= 26; loop++)
        {
          System.out.println(ch + " : " + letters[loop]);
          ch++;
        }
        System.out.println();
        System.out.println("Total letters = " + total);
      }
    }
    1. Each line in the text file is read in and then each character in the line is copied into ch. If ch is an uppercase letter, it is converted to its lowercase counterpart.
    2. If the character is a letter, the ASCII value of the letter is adjusted to fit the range from 1-26. For example, if ch == 'a', the program solves 97 - 96 = 1. Then the appropriate cell of the array is incremented by one.
    3. Again, position 0 in the array is not used to make the data processing easier.


Lesson MenuPreviousNext
Contact
 ©ICT 2003, All Rights Reserved.