title: What is causing this StringIndexOutOfBoundsException?
body: <p>I am writing a program that takes names from a text file and prints the initials of the names.</p>
  
  <p>The names are in "Last, First M." format, with each name being on a separate line.</p>
  
  <p>Because the text file contains the names of everybody in my class I am not including it in this post.</p>
  
  <p>The error I am receiving is:
  Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
      at java.lang.String.substring(String.java:1963)
      at AS01a.main(AS01a.java:28)</p>
  
  <pre><code>/* My Name
  ** February 6, 2019
  ** Class Name
  ** Assignment Name
  ** Collaborations: None
  */
  import java.io.File;
  import java.io.FileNotFoundException;
  import java.util.Scanner;
  
  public class AS01a{
     public static void main(String args[]) throws FileNotFoundException{
        String DEFAULT_FILE_NAME = "AS01.txt";
        String fileName;
        if (args.length != 0)  
           { fileName = args[0]; }
        else
           { fileName = DEFAULT_FILE_NAME; }
  
        Scanner input = new Scanner(new File(fileName));
        String fullName, first, middle, last, initials;
  
        while(input.hasNextLine()){
  
           fullName = input.nextLine();
  
           //Dividing the full name into individual initials
           first = fullName.substring(fullName.indexOf(" ")+1, fullName.indexOf(" ")+2);
           middle = fullName.substring(fullName.lastIndexOf(" ")+1, fullName.lastIndexOf(" ")+2);
           last = fullName.substring(0,1);
  
           //Testing to see if the full name contains a middle name
           if(fullName.indexOf(" ") == fullName.lastIndexOf(" ")){
              initials = first + ". " + last + ".";
           }
  
           else{
              initials = first + ". " + middle + ". " + last + ".";
           }
  
           if(input.hasNextLine()){
              System.out.println(fullName + " yields " + initials);
           }  
        }
     }
  }
  </code></pre>
  
  <p>My results come out as expected, the only problem is the errors previously mentioned.</p>