Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Java Scanner Class

So far my program only runs properly if the user enters their first and last name separated by a space, however if the user enters their middle name, the program has no way to recognize the difference. Any ideas on how to catch possible exceptions?

package com.javaprojects;

// import packages
import java.util.Scanner;


public class Main {

    /*
    This program makes use of built-in subroutines when working with Strings.
    It asks the user for their first name and last name separated by a space.
    Read the input from the user through indexOf() subroutine to find position
    of the space, then use substring() to extract each of the two names. In
    addition, output the number of characters in each names and output of the
    user's initials.
     */

    /*
    -outline variables
    use indexing to find the space to split on
    save the first and last name to variables
    output total characters in first and last name
    use indexing to print first and last initials

     */

    public static void main(String[] args) {

        // state variables
        Scanner stdin;
        String firstName;
        String lastName;
        String firstInitial;
        String lastInitial;
        String initials;
        int firstNameCount;
        int lastNameCount;

        // define variables
        System.out.println("Hello there, please enter your first and last name separated by a space : ");

        // read user input
        stdin = new Scanner (System.in);
        firstName  = stdin.next();
        lastName = stdin.next();
        System.out.println(firstName);
        System.out.println(lastName);

        // define variables firstNameCount and lastNameCount
        firstNameCount = firstName.length();
        lastNameCount = lastName.length();

        // define variable to parse initials
        firstInitial = firstName.substring(0, 1);
        lastInitial = lastName.substring(0, 1);
        initials = firstInitial + " " + lastInitial;

        // use index to parse first and last name, storing the first letters in variable initial
        System.out.println("First Name : " + firstName);
        System.out.println("Last Name : " + lastName);
        System.out.println("There are " + firstNameCount + " in your first name");
        System.out.println("There are " + lastNameCount + " in your last name");
        System.out.println("Your initials are : " + initials);

    }
}