Q:

Printing separate name variations in java?

I am making a programming to print the following
user inputs name like so --> first middle last
   prints: 
   FML
   Variation one: LAST, First M.
   Variation two: Last, First Middle
Now, I need an if statement so that if just a first name is entered it says "error, incorrect input"
I coded this horribly and extremely unconventional, but hey, this is the first thing I've ever programmed before, so I guess we all start somewhere.
import java.util.Scanner;

public class name {
    public static void main(String[]args)
{
    Scanner input = new Scanner(System.in);
    String fullName = input.nextLine();
    String firstName;
    String middleName;
    String lastName;

    //Declares length of entire name
    int nameLength = fullName.length();

    //Declares int where first space is
    int a = fullName.indexOf(" ");

    //Declares int where second space is
    int b = fullName.lastIndexOf(" ");

    //If they equal each other, then there is only one space
    if ( a == b )
    {
        firstName = fullName.substring(0,a);

        lastName = fullName.substring(a+1,nameLength);

        String firstNameInitial = firstName.substring(0,1);
        String lastNameInitial = lastName.substring(0,1);
        String upperCaseInitials = (firstNameInitial.toUpperCase() + lastNameInitial.toUpperCase());

        firstName = fullName.substring(0,a);

        lastName = fullName.substring(b+1,nameLength);

        System.out.println("Your initials are: " + upperCaseInitials);
        System.out.println("Variation One: " + lastName.toUpperCase() + ", " + firstNameInitial.toUpperCase() + firstName.substring(1,a));
        System.out.println("Variation Two: " + lastNameInitial.toUpperCase() + lastName.substring(1,lastName.length()) + ", " + firstNameInitial.toUpperCase() + firstName.substring(1,a));

    }

    //If a < b then it will notice a middle name exists due to multiple spaces
    else if ( a < b )
    {
        firstName = fullName.substring(0,a);

        middleName = fullName.substring(a+1,b);

        lastName = fullName.substring(b+1,nameLength);

        String firstNameInitial = firstName.substring(0,1);
        String middleNameInitial = middleName.substring(0,1);
        String lastNameInitial = lastName.substring(0,1);
        String upperCaseInitials = (firstNameInitial.toUpperCase() + middleNameInitial.toUpperCase() + lastNameInitial.toUpperCase());

        //MNIC = Middle Name Initial Capitalized
        String MNIC = middleNameInitial.toUpperCase();

        //MNIMFC = Middle Name Initial Minus First Character
        String MNIMFC = middleName.substring(1, middleName.length());

        System.out.println("Your initials are: " + upperCaseInitials);
        System.out.println("Variation One: " + lastName.toUpperCase() + ", " + firstNameInitial.toUpperCase() + firstName.substring(1,a) + " " + middleNameInitial.toUpperCase() + "." );
        System.out.println("Variation Two: " + lastNameInitial.toUpperCase() + lastName.substring(1,lastName.length()) + ", " + firstNameInitial.toUpperCase() + firstName.substring(1,a) +  " " + MNIC  + MNIMFC);

    }

}

}

A:

You can just add this check
if(fullName.indexOf(" ")==-1 || (fullName.indexOf(" ") == fullName.lastIndexOf(" "))){
    // The first check is to check if only firstname was given and the second check is to check if only first and middle names were given.
    // If first + middle is a valid scenario, you can remove the second half of the if condition
    System.out.println("error, incorrect input");
    System.exit(0);
}

before the below statement in your code.
int nameLength = fullName.length();

