Java Notes Prev: Java Example: Dialog: Take me to your leader
Java Example: Dialog: 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 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 variable uses is small, which makes it easier on the human reader.
- Declare at the front. Advantages:
- This leaves space on the remainder of the line to add a comment. 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 as important.
- It 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 your instructor requires. 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); } } |