title: String index out of range error 7
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/9020859/string-index-out-of-range-n">String index out of range: n</a>  </p>
  </blockquote>
  
  
  
  <p>I'm writing a program to generate a username based off of a user's inputs (first, middle, and last names). I'm supposed to get the first character from each name(first, middle, and last) as well as the last character of the last name in order to generate a username. I've successfully wrote the program to generate the first character of each name, but when I tried to get my program to generate the last character of the last name I would get this error:</p>
  
  <p>Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 7</p>
  
  <pre><code>at java.lang.String.charAt(String.java:658)
  at UsernameGenerator.main(UsernameGenerator.java:39)
  </code></pre>
  
  <p>Here is my code:</p>
  
  <pre><code>    import java.util.Scanner;
      /**
      UsernameGenerator.java
      Generates a username based on the users inputs.
      @author: Evan Fravert
      */
  public class UsernameGenerator {
  /**
    * Generates a username based on the users inputs.
    *@param args command line argument
    */
    public static void main(String[] args)
  { // abcde
    String first;
    String middle;
    String last;
    String password1;
    String password2;
    int randomNum;
    randomNum = (int) (Math.random() * 1000) + 100;
    Scanner userInput = new Scanner(System.in);
    System.out.println("Please enter your first name:");
    first = userInput.nextLine();
    String firstLower = first.toLowerCase();
    System.out.println("Please enter your middle name:");
    middle = userInput.nextLine();
    String middleLower = middle.toLowerCase();
    System.out.println("Please enter your last name:");
    last = userInput.nextLine();
    int lastEnd = last.length();
    String lastLower = last.toLowerCase();
    System.out.println("Please enter your password:");
    password1 = userInput.nextLine();
    System.out.println("Please enter your password again:");
    password2 = userInput.nextLine();
    char firstLetter = firstLower.charAt(0);
    char middleLetter = middleLower.charAt(0);
    char lastLetter = lastLower.charAt(0);
    char lastLast = lastLower.charAt(lastEnd);
    if (first == null || first.length() &lt;= 0) {
        firstLetter = 'z';
    }
    else {
    firstLetter = firstLower.charAt(0);
    }
    System.out.println("Your username is " + firstLetter + ""
    + middleLetter + "" + lastLetter + "" + "" + lastLast + "" + randomNum); 
    System.out.println("Your password is " + password1);
    System.out.println("Welcome " + first + " " + middle + " " + last + "!");
    }
    }
  </code></pre>
  
  <p>Thanks in advance!</p>