Lecture 15 (Tychonievich) - CSV and Arrays

Lecture Date: Monday, February 16

One common family of files is spreadsheets. The simplest format for spreadsheets is the “Comma Separated Value” or CSV. The idea is that each row is its own line and each cell of each row is separated by some character, such as a comma or semicolon.

We can get a row at a time using ourScanner.nextLine(). To get the cells in the row, we use the String method line.split(';') which splits a line around the given character (in this example, around ;s).

The result of split is a type of value called an array of Strings, written in java as String[]. This is the second time we’ve seen String[]: we also saw it in public static void main(String[] args).

Consider an array int[] x

  • You can make an array value to put in this variable using new int[5], where 5 is the length of the list of ints to create.
  • You can alternatively make an array value to put in x using curly braces: x = { 3, 1, 4, 1, 5 };
  • x acts like a bunch of numbered variables, x[0], x[1], etc.
  • The index part of those numbered variables can be computed on the fly, like x[i+3]
  • The variable x is the entire list; it also has one other field, x.length (note: no parantheses)
  • You can’t change the length of an array once you make it. There’s no add, remove, insert, etc.
  • Arrays have no useful methods; they can’t even be printed easily.
  • There is a class Arrays that is kind of like Math: you can’t make a new one like you can Scanner and Random, but it does have a bunch of useful methods in it, such as Arrays.toString(x) for printing an array nicely and Arrays.sort(x) for reordering the values in an array into ascending order.

Data files:

From Lecture: