0

Hi guys. First post. I have to write a program that allows the user to input his or her name. If the user type all lowercases the program should be able to convert to first letter of the name to uppercase. Also, if the user press "enter" without entering the name then the program has to display a message saying that the field is required. My problem is : i have the uppercase part right, but if i press enter without entering the name then i get a lot of errors. What do you suggest?
Thanks for your help.
here is the code corresponding to that part:

public void actionPerformed(ActionEvent e) {


firstNameAsEntered = firstNameEntry.getText().trim();


c= firstNameAsEntered.charAt(0);
firstNameAsEntered= Character.toUpperCase(c) + firstNameAsEntered.substring(1);
System.out.println(firstNameAsEntered);


if (firstNameAsEntered.length() == 0)
System.out.println("First name is required");
}
});
return firstNameEntry;
} // end private JTextField getFirstNameEntry()

Edited by happygeek: fixed formatting

2
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by studecua
0
c= firstNameAsEntered.charAt(0)

This will surely fail if the character string is empty ...

first check the length of the string, before translating the first letter into uppercase, and maybe ask yourself if the string may be null ...

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.