Ryan
Ryan

Reputation: 57

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);


    }







}

}

Upvotes: 0

Views: 2168

Answers (5)

Popsyjunior
Popsyjunior

Reputation: 305

since it's your first attempt, I'll give you a modified version of your code:

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(" ");

        /*** Use the split function to split the names with spaces as delimiter **/
        String[] n = fullName.split(' ');

        firstName = n[0];

        if( n.length == 2 ) {
            lastName = n[1];
        }

        if( n.length > 3 ) {
            lastName = n[1];
            middleName = n[2];
        }

        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);
    }
}

Upvotes: 0

LuigiEdlCarno
LuigiEdlCarno

Reputation: 2415

You can use the String.split() function to split a String into its parts along a seperator.

In your case that would be the space (" ")

Try:

Scanner input = new Scanner(System.in);
String fullName = input.nextLine();
String firstName;
String middleName;
String lastName;

String[] parts = fullName.split(" ");

if(parts.length() == 3){
    // 3 words were entered, so there is a middle name
}
// ...

Upvotes: 1

Trey Jonn
Trey Jonn

Reputation: 360

You can narrow down to the condition you stated by following these steps:

  1. Trim the input String fullName before statement int a = fullName.indexOf(" ");
  2. Next check if the index of whitespace (i.e. value of a and b variables) is -1, then you can assume that the input contains only a single word, presumably Firstname
  3. Print the error message "error, incorrect input"

Upvotes: 0

Rahul
Rahul

Reputation: 45060

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();

Upvotes: 0

Bernhard Barker
Bernhard Barker

Reputation: 55599

You can simply check for a == -1. indexOf returns -1 if not found (as per the docs).

if (a == -1)
   System.out.println("Error, invalid input!");
else if (a == b)
...

Upvotes: 0

Related Questions