Java Lecture 4

The Args.length thing

  1. If your parameter to 'main' is Args, then Args.length is an integer value that is the number of arguments that your program was started with
  2. So, if your program is started like 'java lab6d 34 6.5 hello', Args.length will be 3 (the three arguments are 34, 6.5, and hello)
  3. You can use this directly in an if statement, like 'if (Args.length == 1) ...'

If and relational expressions

  1. p95 (sec 3.5) has a list of them
  2. Like ML, except equality test is == and not equals is !=
  3. Later we will see the logical operators
    1. p183, sec 5.2
    2. AND (ML andalso) is &&
    3. OR (ML orelse) is ||
    4. NOT (ML not) is !
    5. && and || act like ML ones, the second side is only evaluated if it has to be

Block statements and single statements

  1. A block of statements is a list of statements surrounded by curly braces
  2. A block can occur anywhere a single statement can occur
  3. This can lead to subtle bugs, like having a semicolon after an if expression

Examples writing Java programs

  1. A min3 function

The while loop

  1. Exact same structure as the if statement, with the word 'while'
  2. Except, it repeats, instead of just doing the statement(s) once like an if
  3. As with recursion, the condition must be getting "smaller" or closer to false as you go

Keyboard Input

  1. Have to have the "import java.io.*;" statement at the top of your program
  2. Have to have the words "throws IOException" after main(String args[]) and before the opening curly brace
  3. Have to have the line
    BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in));
  4. Have to declare a String variable like "String line;"
  5. Now, can do
    line = stdin.readLine();
  6. This will read a line of keyboard input into the String variable line
  7. Do it again to read the next line, and so on
  8. Input is in Strings; if you need numbers, have to convert it

Examples 1 and 2; and sample runs.