Basic Idioms

Static nested class

Static nested classes define logically related types. It enables the nested class to have access to the private members of the enclosing class. This is often used with iterators. For example,

// A deck of playing cards.
public class Deck {
    private List<Card> cardList = new ArrayList<Card>(52);
    private int topOfDeck = 0;

    public class DeckIterator {
	      private Deck d_deck;
	      private int nextIndex;
// the implementation of DeckIterator has full access
// to the Deck's cardList and topOfDeck data members.
 ...

DeckIterator acts like any top-level class although it is referenced as Deck.Deckiterator.

Input / output

Read from a file

The Scanner class provides the highest level interface and is usually the most convenient way to read and parse input data.

Scanner inp = new Scanner( new File( "fileName" ));
while( inp.hasNext() ) {
    String line = inp.nextLine();
	String[] word = line.split( " " );
	for( int i = 0; i != word.length; i++ )
        // use word[i].trim()
      }