Prev: Dialog Box Output | Next: Dialog Box Input Loop
Java Notes
Example: Capitalize
This program reads a word, makes the first letter upper case and the remainder lower case, and outputs it. The input-process-output organization is very common in simple programs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
// File : dialog/capitalize/Capitalize2.java
// Purpose: Make first letter upper case, remainder lower case.
// Uses traditional input-process-output organization.
// Author : Fred Swartz
// Date : 30 Mar 2006
import javax.swing.*;
public class Capitalize2 {
public static void main(String[] args) {
//.. Input a word
String inputWord = JOptionPane.showInputDialog(null, "Enter a word");
//.. Process - Separate word into parts, change case, put together.
String firstLetter = inputWord.substring(0,1); // Get first letter
String remainder = inputWord.substring(1); // Get remainder of word.
String capitalized = firstLetter.toUpperCase() + remainder.toLowerCase();
//.. Output the result.
JOptionPane.showMessageDialog(null, capitalized);
}
}
|
Error handling - Happy Trails
This is a Happy Trails program, one that doesn't worry about bad input. What could be bad? It wouldn't work if the empty string (string with zero characters in it) was used as input, or the cancel button or the close box was clicked. In later programs we'll add a little extra code to handle these cases.
Where to declare local variables: at beginning or at first use?
Local variables can be declared anywhere before they are used. There are two styles of declaration.
- Declaring variables when they are first used.
This style has several advantages.
- It guarantees that variables are initialized.
- No unused variables are declared.
- It's clear what the type is at the time it is used.
- The distance between uses of a variable should be as small as possible to make it easier on the human reader.
Comments sometimes don't fit on the remainder of the line so they must be put on the line before if required, but good variable names often mean that comments are not necessary.
- At the front. This style leaves space on the remainder of the line to add a comment. It also lists the cast of characters at the front, which some programmers find helpful.
Clarity first. Use whichever style makes the program clearer for you, or whichever style you instructor requires. I'm flexible, but you will find most of my programs use the declare-at-first-use style.
Same program with variables declared at beginning.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
// File : examples-dialog/Capitalize.java
// Purpose: Capitalize program style variation.
// Variables declared at beginning.
// Author : Fred Swartz
// Date : 09 Oct 2005
import javax.swing.*;
public class Capitalize {
public static void main(String[] args) {
//.. Local variable declarations.
String inputWord; // Input will be read into this variable.
String firstLetter; // Assigned the first letter of the input.
String remainder; // Gets everything after the first letter.
String capitalized; // Put the correct cased parts together.
//.. Input a word
inputWord = JOptionPane.showInputDialog(null, "Enter a word");
//.. Process - Separate word into parts, change case, put together.
firstLetter = inputWord.substring(0,1); // Get first letter
remainder = inputWord.substring(1); // Get remainder of word.
capitalized = firstLetter.toUpperCase() + remainder.toLowerCase();
//.. Output the result.
JOptionPane.showMessageDialog(null, capitalized);
}
}
|
Capitalize program using console-style input-output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
// File : dialog/capitalize/CapitalizeConsole.java
// Purpose: Capitalize program version which reads from console.
// Author : Fred Swartz
// Date : 30 Mar 2006
import java.util.*;
public class CapitalizeConsole {
public static void main(String[] args) {
//.. Create and initialize a scanner to read from the console.
Scanner in = new Scanner(System.in);
//.. Input a word
System.out.println("Enter a word");
String inputWord = in.next(); // Reads one "word"
//.. Process - Separate word into parts, change case, put together.
String firstLetter = inputWord.substring(0,1); // Get first letter
String remainder = inputWord.substring(1); // Get remainder of word.
String capitalized = firstLetter.toUpperCase() + remainder.toLowerCase();
//.. Output the result.
System.out.println(capitalized);
}
}
|