Evan F
Evan F

Reputation: 61

StringIndexOutOfBoundsException: String index out of range 0

I'm trying to write a program to take the first letter of the user input to generate a username. I'm trying to write it so that if the user leaves the input blank, then the letter that would otherwise be taken to generate the username defaults to the letter 'z'.

Here is my full 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()-1;
  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.length() == 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 + "!");
}
}

Upvotes: 0

Views: 1437

Answers (3)

Sivaraman
Sivaraman

Reputation: 61

u getting the exception from this line

char firstLetter = firstLower.charAt(0);

below one is enough to get the first letter. So keep this only

char firstLetter;
if (first.length() == 0) {
      firstLetter = 'z';
  }
  else {
  firstLetter = firstLower.charAt(0);
  }

In the same way u have to check other input string values

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500525

This isn't going to work:

char firstLetter = firstLower.charAt(0);
...

if (first.length() == 0) {
    firstLetter = 'z';
}

If the length is 0, then charAt(0) will throw the exception in the title. You could do this:

char firstLetter = first.length() == 0 ? 'z' : firstLower.charAt(0);

Upvotes: 2

Paul Bellora
Paul Bellora

Reputation: 55213

The exception is likely being thrown here:

char firstLetter = firstLower.charAt(0);

You are calling charAt(0) before checking to see if the input was empty. Try something like this instead:

char firstLetter = first.isEmpty() ? 'z' : firstLower.charAt(0);

Note that first.isEmpty() is an equivalent expression to first.length() == 0.

Upvotes: 1

Related Questions