0

I am trying to create a code that accepts a user's full name and returns first and last names and initials. Since a user's name length varies, I did not want to use hard coding, so I extract names and initials programmatically. However when I run it and enter a name, I get the following error message:

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

I looked into my code carefully and cannot see where exactly I miscalculated on the index range. I tried to find similar questions here, but though I did see similar problems, they have to do with C++ or Perl, not Java.

package nameSubstring;

import java.util.Scanner;

public class NameSubstring {

    public static void main(String[] args) {

        /*
         * This is a program that accepts a user’s full name as a string (e.g. Margaret Thatcher) and displays to the user his/her first name, last name and initials in the following format:
           Your first name is Margaret and your last name is Thatcher and your initials are MT.
             */

        System.out.println("This program will take your full name and display your first name, last name, and initials.");
        Scanner scanner = new Scanner(System.in); 
        String firstName, lastName, firstNameInitial, lastNameInitial; 
        System.out.println("Please enter your full name, e.g. Jane Smith:");
        String fullName = scanner.next();
        int nameSpace = fullName.indexOf(' '); 
        firstName = fullName.substring(0, nameSpace); 
        lastName = fullName.substring(nameSpace)+1; 
        firstNameInitial = firstName.substring(0, 1);
        lastNameInitial = lastName.substring(0, 1);
        System.out.println("Your first name is " + firstName + ", " + "your last name is " + lastName + ", " + "and your initials are " + firstNameInitial + lastNameInitial + ".");

    }
}
5
  • 2
    Instead of index you can use String split(), like fullName.split(" "). Therefore, It will have array of values. Commented Jun 27, 2019 at 17:43
  • Something to consider: Personal names around the world Commented Jun 27, 2019 at 17:47
  • 3
    Side point: do not use the term "Java script" in this context.. This is not a Java script; it is a Java program. JavaScript is a completely different language. Commented Jun 27, 2019 at 18:33
  • @FredK, good point. Thanks for noting this. I'll try to refrain from using script in the context of Java to avoid confusion with JavaScript. Commented Jun 28, 2019 at 1:42
  • @Andreas, thanks for recommending and posting the "name convention" article! It was very interesting and informative. Commented Jun 28, 2019 at 14:36

1 Answer 1

2

Instead of next() use nextLine():

String fullName = scanner.nextLine();

and correct the error with the +1 which must be inside the parenthesis:

lastName = fullName.substring(nameSpace+1);
Sign up to request clarification or add additional context in comments.

2 Comments

thank you so much!!! Your corrections solved the problem, and I almost feel like an idiot for not realizing what was wrong. I REALLY appreciate your prompt help.
No need to feel like an idiot. These are errors everybody does when starting something new. Just remember that debugging and reading the documentation is the only way to learn (apart from SO of course).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.