L.Jones28 L.Jones28 - 1 month ago 10
Java Question

Trying to split up a string with blank space

I'm writing out a piece of a code that where I am trying to split up the user's input into 3 different arrays, by using the spaces in-between the values the user has entered. However, everytime i run the code i get the error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at Substring.main(Substring.java:18)
Java Result: 1


I have tried to use a different delimiter when entering the text and it has worked fine, e.g. using a / split the exact same input normally, and did what i wanted it to do thus far.
Any help would be appreciated!
Here's my code if needed

import java.util.Scanner;

public class Substring{
public static void main(String[]args){
Scanner user_input = new Scanner(System.in);

String fullname = ""; //declaring a variable so the user can enter their full name
String[] NameSplit = new String[2];
String FirstName;
String MiddleName;
String LastName;

System.out.println("Enter your full name (First Middle Last): ");
fullname = user_input.next(); //saving the user's name in the string fullname

NameSplit = fullname.split(" ");//We are splitting up the value of fullname every time there is a space between words
FirstName = NameSplit[0]; //Putting the values that are in the array into seperate string values, so they are easier to handle
MiddleName = NameSplit[1];
LastName = NameSplit[2];

System.out.println(fullname); //outputting the user's orginal input
System.out.println(LastName+ ", "+ FirstName +" "+ MiddleName);//outputting the last name first, then the first name, then the middle name
new StringBuilder(FirstName).reverse().toString();
System.out.println(FirstName);


}


}

Answer

You have made mistakes at following places:

fullname = user_input.next();

It should be nextLine() instead of just next() since you want to read the complete line from the Scanner.

String[] NameSplit = new String[2];

There is no need for this step as you are doing NameSplit = user_input.split(...) later but it should be new String[3] instead of new String[2] since you are storing three entries i.e. First Name, Middle Name and the Last Name.

Here is the correct program:

class Substring {
    public static void main (String[] args) throws java.lang.Exception {
        Scanner user_input = new Scanner(System.in);
        String[] NameSplit = new String[3];
        String FirstName;
        String MiddleName;
        String LastName;

        System.out.println("Enter your full name (First Middle Last): ");
        String fullname = user_input.nextLine();

        NameSplit = fullname.split(" ");
        FirstName = NameSplit[0];
        MiddleName = NameSplit[1];
        LastName = NameSplit[2];

        System.out.println(fullname);
        System.out.println(LastName+ ", "+ FirstName +" "+ MiddleName);
        new StringBuilder(FirstName).reverse().toString();
        System.out.println(FirstName);
    }
}

Output:

Enter your full name (First Middle Last): John Mayer Smith

Smith, John Mayer

John