Notes for Project 1

java.util.Scanner

Here is Listing 3-2 from Section 3.7 of the book (page 77 of the 9th ed.):
import java.util.*;

public class InputTest {
    public static void main(String[] args) {
	Scanner in = new Scanner(System.in);

	System.out.print("What is your name? ");
	String name = in.nextLine();

	System.out.print("How old are you? ");
	int age = in.nextInt();

	System.out.println("Hello, " + name + ". Next year you'll be "
			  + (age + 1));
    }
}
This code shows an example of using the Scanner class. You can see the online documentation for the Scanner class at http://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html Note that this link is to the "NO FRAMES" version of the API so that you can go directly to the Scanner documentation. I usually use the "FRAMES" version and select "java.util" in the upper left frame, then "Scanner" in the lower right frame.

Be careful to not make more than one scanner for the same stream in a program.

You can compile this program using a command like this:
   javac InputTest.java
and then run it with a command like this:
   java InputTest

Since the Scanner class was not added to the Java API until version 1.5 (or 5.0), you will need at least version 1.5 of the compiler and interpreter to compile and run this program. On the class page there is a link you can use to download the Java SDK, which includes both the compiler and the interpreter. If you look in the Installation Instructions for Windows, you can read about how to update the PATH environment variable in Windows.

The String.split method

Here is a link to the "NO FRAMES" version of the documentation for the String class (which is in the java.lang package):
http://docs.oracle.com/javase/8/docs/api/java/lang/String.html
If you scroll down the page you will find a link to the split method and the method's explanation.

The split method takes a String object as its parameter and returns an array of String objects. The parameter is a regular expression. For Project 1, you can use a regular expression like this: " +" which will match any substring of one or more spaces.

Here is a program that uses String.split to break a string up into words and then prints each word:

public class Split {
    public static void main(String[] args) {
	String str = "Groucho Harpo   Chico  Zeppo";
	String[] names = str.split(" +");

	for (int i = 0; i < names.length; i++) {
	    System.out.println(names[i]);
	}
    }
}

Arrays

Since the String.split method returns an array of strings, you should read about how to use arrays in Java. In the textbook you can read about arrays in Chapter 3, starting on page 81. On the web you can look at the Java Tutorial about arrays:
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html

One important difference between arrays in Java and arrays in C++ is the way that you create arrays. In C++ a line like this
   int array[10];
creates an array of 10 integers and you can immediately access elements using a statement like this:
   array[0] = 42;

To do the equivalent thing in Java, you need a line like this:
   int[] array = new int[10];
In this case, array is not actually an array, it is a reference that can refer to array objects. We will talk about this more in class, but for now keep in mind that you need to use new to create an array in Java. Note that when you use String.split you are not creating an array, you are referring to an array that has been created inside the split method.

In Java, every array has a length attribute which tells you how many elements are in the array. In the sample code for String.split above, you can see the length attribute used to control the for loop.