Lecture 9 (Edwards) - Arrays and Files

Lecture Date: Wednesday, February 11

So far we have only given our programs input from the keyboard. This is fine but what if we want to input hundreds or even thousands of peices of information. This would be unbearable for a human.

Today we will learn how to store several of the same type of data (ints for example) in an array

After this we will talk about bringing in data from a file and processing the data.

Here are some date sets that you should download:

Before we use this data, create a file called ‘className.csv’ and in this file put the name of the few people you are seating next to.

Then run something similar to this.

ClassName.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import java.io.File;
import java.util.Scanner;

public class Classmates {

  public static void main(String[] args) throws Exception {
      Scanner inputFile = new Scanner(new File("classmates.csv"));
      while(inputFile.hasNextLine()) {
          String line = inputFile.nextLine();
          System.out.println(line);
      }

  }

}

Reading data from files is very similar to reading from a keyboard. In fact we will use many of the same methods.

If we have time we will look at data from this website: http://stardock.cs.virginia.edu/louslist/Courses/view/CS

We’ll look at some examples in class and post the code here.

Code: