# Java: Programming - Transform Name - Answer

## Solution to: Write a program to redisplay a name, possibly transformed.

This Java program will reformat a user's name from "First Last" to "Last, First". There are two cases that are handled: the trivial case of a single name, where the name doesn't have to be changed (eg, the single-name pop star "Madonna"), and the case where the names must be rearranged.

## Iterative programming

Here is a possible iterative development sequence to create this program, starting with something really simple, and building to the complete program. It uses JOptionPane for input and output.

## Iteration 1 - Program that does nothing

    // File   : nametransform/FirstLast.java
    // Purpose: Read and display a name.
    // Author : Fred Swartz
    // Date   : 2005-03-01

    public class FirstLast {

        public static void main(String[] args) {
        }

    }

It compiles, and does exactly nothing! But now you are sure your file name and the class name match, and that everything is probably spelled right. I often start compiling with something as simple as this. Now let's make it do a little.

## Iteration 2 - Read input

    // File   : nametransform/FirstLast.java
    // Purpose: Read and display a name.
    // Author : Fred Swartz
    // Date   : 2005-03-01

    import javax.swing.JOptionPane;

    public class FirstLast {

        public static void main(String[] args) {
            String fullName;

            fullName = JOptionPane.showInputDialog(null, "Enter your name");

            System.exit(0);
        }

    }

This added several things.

1. The `import` to tell the compiler that it should look for JOptionPane symbols in the `javax.swing` package (directory).
2. I've declared a String variable to hold the name.
3. A call on `showInputDialog` to read a string from the user and store the result into the `fullName` varible.
4. A call to `System.exit(0)` is required to stop execution on some Java systems, so I usually add it.

It might take a few compilations to get this running. I forgot something the first time and had to recompile it. Don't be surprised if each of these small iterations take a few tries to get running. Here's what it produced. |

## Iteration 3 - Show output

I've dropped the comments at the top from these listings just to save space, but they're still there in the actual file. I've added comments in the body of main.

    import javax.swing.JOptionPane;

    public class FirstLast {

        public static void main(String[] args) {
            String fullName;

            //... Read a name from the user.
            fullName = JOptionPane.showInputDialog(null, "Enter your name");

            //... Show the result.
            JOptionPane.showMessageDialog(null, "Your name is " + fullName);

            //... Stop - required in some systems.
            System.exit(0);
        }

    }

The only change was to add the `showMessageDialog`, to show the input name with no changes. Here's what it produced. |

## Iteration 4 - Find a blank between names, reverse

    import javax.swing.JOptionPane;

    public class FirstLast {

        public static void main(String[] args) {
            String fullName;        // Original "First Last"
            String firstName;
            String lastName;
            String finalName;       // Final "Last, First"
            int    blankPosition;   // Index of blank or -1

            //... Read a name from the user.
            fullName = JOptionPane.showInputDialog(null, "Enter your name");

            //... Find a blank and reformat the name.
            blankPosition = fullName.indexOf(" ");
            firstName     = fullName.substring(0, blankPosition);
            lastName      = fullName.substring(blankPosition+1);
            finalName     = lastName + ", " + firstName;

            //... Show the result.
            JOptionPane.showMessageDialog(null, "Your name is " + finalName);

            //... Stop - required in some systems.
            System.exit(0);
        }

    }

This looks for a blank in the name with `indexOf`. It then extracts the first and last names using `substring` and the position of the blank. Here's what it produced. There is still one problem tho - if the name doesn't have a blank in it, the value returned by `indexOf` is -1, which will cause a "StringIndexOutOfBoundsException" if used by `substring` because it isn't a legal position in a string. |

## Iteration 5 - Testing for a blank

Here's the final version. It tests the value from `indexOf`. If it's -1, there must not have been a blank, so just use the original input name for the result. If it's not -1 (ie, the else clause), use the blank position to extract the first and last names as above.

    // File   : nametransform/FirstLast.java
    // Purpose: Read and display a name.
    // Author : Fred Swartz
    // Date   : 2005-03-01

    import javax.swing.JOptionPane;

    public class FirstLast {

        public static void main(String[] args) {
            String fullName;        // Original "First Last"
            String firstName;
            String lastName;
            String finalName;       // Final "Last, First"
            int    blankPosition;   // Index of blank or -1

            //... Read a name from the user.
            fullName = JOptionPane.showInputDialog(null, "Enter your name");

            //... Look for a blank and decide what to do.
            blankPosition = fullName.indexOf(" ");
            if (blankPosition == -1) {
                //... Do nothing if it's only a single name.
                finalName = fullName;
            } else {
                //... Reverse the first and last names.
                firstName     = fullName.substring(0, blankPosition);
                lastName      = fullName.substring(blankPosition+1);
                finalName     = lastName + ", " + firstName;
            }

            //... Show the result.
            JOptionPane.showMessageDialog(null, "Your name is " + finalName);

            //... Stop - required in some systems.
            System.exit(0);
        }

    }