I am writing a Results Letter for my class, the application should prompt the user for a
Question:
I am writing a Results Letter for my class, the application should prompt the user for a patient's first and last names. The application should then extract these names so that a result letter can be constructed. After the application prompts the user to enter a name, a loop control variable is initialized to 0. While the variable remains less than the length of the entered name, each character is compared to the space character. When a space is found, two new strings are created. The first, firstName, is the substring of the original entry from position 0 to the location where the space was found. The second, familyName, is the substring of the original entry from the position after the space to the end of the string. However it is not running correctly in my java compiler, what am I missing?
HERE IS MY CODE BELOW:
import javax.swing.*;
public class ResultsLetter {
public static void main(String[] args) {
String name;
String firstName = "";
String familyName = "";
int x;
char c;
name = JOptionPane.showInputDialog(null, "Please enter patient's first and last name");
x = 0;
while(x < name.length()) {
if(name.charAt(x) == ' ') {
firstName = name.substring (0, x);
familyName = name.substring(x + 1, name.length());
}
++x;
}
JOptionPane.showMessageDialog(null,
"Dear " + firstName +
"\nYour results for your Papsmear that you had done have come back normal." +
"\nPlease feel free to call our office at 479-271-0005 if you have any further questions." +
"\nPlease make sure you are scheduled for your annual exam yearly and pap smear every 3 years." +
"\nSincerely," +
"\nLifespring Women's Healthcare");
}
}