Results 1 to 4 of 4
- 11-13-2008, 07:41 PM #1
Member
- Join Date
- Oct 2008
- Posts
- 5
- Rep Power
- 0
need help uppercasing names that were input
I can get the first letter of the first name to Capitalize. Then I can get the first letter of the last name to capitalize. My problem is that if the user only inputs a first name then the program can not find a "space" so it gives an error. Is there another way to find a second name? Is there something simple I'm missing?
the line in bold is the one that searches for that space and causes it to fail if there isn't a space
Java code:
private static String enterName()
{
String name = new String(JOptionPane.showInputDialog(null, "What is the name of the new customer:", "INPUT NAME", JOptionPane.QUESTION_MESSAGE));
name = name.trim();
String firstLetter = new String(name.substring(0,1));
firstLetter = firstLetter.toUpperCase();
name = firstLetter.concat(name.substring(1,name.length()) );
String lastNameTest = new String(name.substring(name.indexOf(' ')));
int lastNameTestInt = lastNameTest.length();
JOptionPane.showMessageDialog(null, lastNameTestInt);
if (lastNameTestInt > 0)
{
String firstLetterofLastName = new String(name.substring(name.indexOf(' '), name.indexOf(' ')+2));
firstLetterofLastName = firstLetterofLastName.toUpperCase();
name = name.substring(0, name.indexOf(' ')) + firstLetterofLastName + name.substring(name.indexOf(' ')+2, name.length());
}
return name;
}
- 11-13-2008, 07:46 PM #2
Member
- Join Date
- Oct 2008
- Posts
- 5
- Rep Power
- 0
Here's the error message:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
- 11-13-2008, 09:22 PM #3
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
The error message is telling you that it cannot access the index of a string at index of -1. Check out the String API for the methods you're using.
- 11-13-2008, 09:43 PM #4
Member
- Join Date
- Oct 2008
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
how to record multiple input names and output later in program
By jbajwa1 in forum New To JavaReplies: 4Last Post: 10-02-2008, 10:05 PM -
Getting names of table columns
By Java Tip in forum Java TipReplies: 0Last Post: 01-07-2008, 08:39 AM -
Drive names
By alwz_nikhil in forum New To JavaReplies: 0Last Post: 11-28-2007, 10:04 AM -
how to get the names of the files
By mary in forum Advanced JavaReplies: 2Last Post: 08-05-2007, 04:01 AM -
how to take input and verify input in Java programs
By bilal_ali_java in forum Advanced JavaReplies: 0Last Post: 07-21-2007, 08:46 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks