Prev: Dialog Box Output | Next: Dialog Box Input Loop
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);
}
}
|
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.
Local variables can be declared anywhere before they are used. There are two styles of declaratio