Common Java Tasks

Programmers make no secret of the fact that they most often work off of known "patterns" (solutions to commonly occurring problems). Let's see some common Java solutions to basic problems. You'll use many of these over and over.

Writing to standard output

Standard output means the console (by default) or the file to which you direct output to with ">". In Java it is called System.out:

public class Example {
    public static void main(String[] args) {
        System.out.println("Hello, World");
    }
}

How does it work?

$ javac Example.java
$ java Example
Hello, World
$ java Example > message.txt

The last line above doesn't echo any response, but it does write "Hello, World" to file named message.txt.

Processing commandline arguments

If you just need them one at a time:

public class Example {
    public static void main(String[] args) {
        for (String s: args) {
            System.out.println(s);
        }
    }
}
$ javac Example.java
$ java Example once upon a time
once
upon
a
time

If the index (position) matters:

public class Example {
    public static void main(String[] args) {
        for (int i = 0; i < args.length; i++) {
            System.out.println("args[" + i + "] = " + args[i]);
        }
    }
}
$ javac Example.java
$ java Example once upon a time
args[0] = once
args[1] = upon
args[2] = a
args[3] = time

Showing a popup message

JOptionPane.showMessageDialog, in its simplest form, takes two parameters: a window to go on top of, and a message to show. If you don't have a window already, just use null.

import javax.swing.JOptionPane;
public class Example {
    public static void main(String[] args) {
        JOptionPane.showMessageDialog(null, "Hello, World");
    }
}

helloworldpopup.jpg

Simple dialog input

Plain old showInputDialog is straightforward:

import static javax.swing.JOptionPane.showInputDialog;
import static javax.swing.JOptionPane.showMessageDialog;

public class Example {
    public static void main(String[] args) {
        String name = showInputDialog("What is your name?");
        showMessageDialog(null, "Hello, " + name);
    }
}

Reading from standard input

Standard input means the console (by default) or the file from which you direct input with "<". In Java it is called System.in. In general you have to use a scanner because System.in by itself is kind of dumb. If you just need one line

import java.util.Scanner;

public class Example {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("What is your name?");
        String name = scanner.nextLine();
        System.out.println("Hello, " + name);
    }
}
$ javac Example.java
$ java Example
What is your name?
Alice
Hello, Alice

Here is an example using redirection to read a whole file, one line at a time.

import java.util.Scanner;

public class Example {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int lineNumber = 1;
        while (scanner.hasNextLine()) {
            String s = scanner.nextLine();
            System.out.println("Line " + lineNumber + ": " + s);
            lineNumber++;
        }
    }
}

If the file "jabberwocky.txt" looks like this:

'Twas brillig, and the slithy toves
Did gyre and gimble in the wabe:
All mimsy were the borogoves,
And the mome raths outgrabe.

Then

$java Example < jabberwocky.txt
Line 1: 'Twas brillig, and the slithy toves
Line 2: Did gyre and gimble in the wabe:
Line 3: All mimsy were the borogoves,
Line 4: And the mome raths outgrabe.

Formatted output

printf can be your friend, even if its use can be ugly and cryptic!

TODO

Trimming a string

TODO

Finding text within a string

TODO

Uppercasing and Lowercasing

TODO

Replacing characters in a string

TODO

Processing each character in a string

TODO

Converting between numbers and strings

TODO

The basic graphics application

TODO

The basic applet

TODO