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()

Recommended Answers

All 2 Replies

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 ...

hey. Thanks for your help. I already solved the problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.