# Java: CMIS 102A Java Summary - v4

Keywords etc
  `import`, `class`, `int`, `double`, `public`, `static`, `void`, `main`, `new`, `if`, `else`, `switch`, `case`, `default`, `break`, `while`, `do`, `for`
Arithmetic operators (result and operands are numeric)
  `+ - * / % ++ --`
Comparison operators (result is boolean, operands numeric)
  `< <= == != >= >`
Logical operators (result and operands are boolean)
  `&& || !`

Assume these declarations for the following method prototypes.

       int i, j;
       double d;
       boolean b;  // For storing true or false value.
       String s, s1, t;
       DecimalFormat df;

## String methods (no import required)

Length
i = | s.length() | length of the string s.
Getting parts
s1 = | s.substring(i, j) | substring from index i to BEFORE index j of s.
s1 = | s.substring(i) | substring from index i to the end of s.
Searching -- Note: `indexOf` returns -1 if the string is not found
i = | s.indexOf(t) | index of the first occurrence of String t in s.
i = | s.indexOf(t, i) | index of String t at or after position i in s.
i = | s.lastIndexOf(t) | index of last occurrence of t in s.
i = | s.lastIndexOf(t, i) | index of last occurrence of t on or before i in s.
Creating a new string from the original
s1 = | s.toLowerCase() | new String with all chars lowercase
s1 = | s.toUpperCase() | new String with all chars uppercase
s1 = | s.trim() | new String with whitespace deleted from front and back
Comparison (note: use these instead of == and !=)
i = | s.compareTo(t) | compares to s. returns <0 if s<t, 0 if ==, >0 if s>t
i = | s.compareToIgnoreCase(t) | same as above, but upper and lower case are same
b = | s.equals(t) | true if the two strings have equal values
b = | s.equalsIgnoreCase(t) | same as above ignoring case
b = | s.startsWith(t) | true if s starts with t
b = | s.startsWith(t, i) | true if t occurs starting at index i
b = | s.endsWith(t) | true if s ends with t

## System methods (no import required)

| System.exit(0); | Stop the program.

## JOptionPane methods - import javax.swing.*

| `JOptionPane.showMessageDialog(null, s);` Displays dialog with message s.
s = | `JOptionPane.showInputDialog(null, s);` Returns user string or `null` if cancelled.
i = | `JOptionPane.showConfirmDialog(null, s);` Returns `JOptionPane.YES_OPTION`, .`JOptionPane.YES_OPTION`, `JOptionPane.NO_OPTION`., `JOptionPane.CANCEL_OPTION`, or `JOptionPane.CLOSED_OPTION`

## Integer methods (no import required) Wu 101

i = | Integer.parseInt(s); | Converts s to integer or throws `NumberFormatException`.

## Double methods (no import required) Wu 102

d = | Double.parseDouble(s); | Converts s to double or throws `NumberFormatException`.

## DecimalFormat methods (import java.text.*) Wu 104

df = | new DecimalFormat("0.00"); | Creates a DecimalFormat object which can be used to format doubles.
s = | df.format(d); | Converts double to String according to the format.

## Math methods (no import required) Wu 114

... | ... | ...

Classes you can ignore: `Date`, `SimpleDateFormat`, `GregorianCalendar`, `BufferedReader`, `InputSreamReader`

Copyleft 2004 Fred Swartz