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 30 31 32 33 34 35 |
// File : data/strings/Initials2.java
// Purpose: Prints intials for either one or two names.
// Name : Fred Swartz
// Date : 2005-10-10
import javax.swing.*;
public class Initials2 {
public static void main(String[] args) {
while (true) {
//.. Terminate loop if no input (cancel or zero-length text).
String name = JOptionPane.showInputDialog(null, "Name?");
if (name == null || name.trim().length() == 0) {
break; // Exit loop if no input.
}
name = name.trim();
//.. First initial
String firstInitial = name.substring(0, 1);
//.. Last intial if there is one.
String lastInitial = "";
int blankPos = name.indexOf(" ");
if (blankPos != -1) { // There is a last name.
lastInitial = name.substring(blankPos+1, blankPos+2);
}
//.. Convert to upper case and display.
String initials = (firstInitial + lastInitial).toUpperCase();
JOptionPane.showMessageDialog(null, "Initial(s): " + initials);
}
}
}
|