/** This program will keep track of a list of mountains in an array. * The purpose of this program is just to practice a little I/O, calling * constructors and arrays (or ArrayLists). */ import java.io.*; import java.util.*; public class Driver { public static void main(String [] args) throws IOException { // We need to do the following: // 1. read the list of mountains from a file called mtn.txt. // Each line of the file has data on one mountain. // 2. create a mountain object with this information // 3. Put this mountain object into an array. // Then, once we have all the mountains in the array, we can do // some calculations, such as finding tallest/shortest (?) BufferedReader in = new BufferedReader(new FileReader("mtn.txt")); //Mountain [] mList = new Mountain []; ArrayList mList = new ArrayList(); int mNumber = 0; while(true) { String line = in.readLine(); if (line == null) break; // At this point, we have one line from the file. StringTokenizer tok = new StringTokenizer(line, ","); String place = tok.nextToken(); String name = tok.nextToken(); int height = Integer.parseInt(tok.nextToken()); // Need to create an object... Mountain m = new Mountain(place, name, height); //mList[mNumber] = m; mList.add(m); ++mNumber; } System.out.println("The following mountains are over 10,000 feet"); in.close(); // Let's print the names of all mountains over 10,000 feet. for (int i = 0; i < mList.size(); ++i) { Mountain m = (Mountain) mList.get(i); if (m.getHeight() > 10000) System.out.println(m.getName() + " height is " + m.getHeight()); } // Now, let's find the average height of all the mountains. int sum = 0; for (int i = 0; i < mList.size(); ++i) { Mountain m = (Mountain) mList.get(i); sum += m.getHeight(); } double average = sum / mList.size(); System.out.printf("\nThe average mountain is %f feet.\n", average); } }