Results 1 to 4 of 4
  1. #1
    jbajwa1 is offline Member
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

    Default 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;
    }

  2. #2
    jbajwa1 is offline Member
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

    Default

    Here's the error message:

    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1

  3. #3
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    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.

  4. #4
    jbajwa1 is offline Member
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

Similar Threads

  1. Replies: 4
    Last Post: 10-02-2008, 10:05 PM
  2. Getting names of table columns
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-07-2008, 08:39 AM
  3. Drive names
    By alwz_nikhil in forum New To Java
    Replies: 0
    Last Post: 11-28-2007, 10:04 AM
  4. how to get the names of the files
    By mary in forum Advanced Java
    Replies: 2
    Last Post: 08-05-2007, 04:01 AM
  5. how to take input and verify input in Java programs
    By bilal_ali_java in forum Advanced Java
    Replies: 0
    Last Post: 07-21-2007, 08:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •