Joe
Joe

Reputation: 31

What is causing this StringIndexOutOfBoundsException?

I am writing a program that takes names from a text file and prints the initials of the names.

The names are in "Last, First M." format, with each name being on a separate line.

Because the text file contains the names of everybody in my class I am not including it in this post.

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)

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

My results come out as expected, the only problem is the errors previously mentioned.

Upvotes: 2

Views: 374

Answers (2)

no id
no id

Reputation: 1672

fullName seems to be an empty string, you can easily check this with debugger. And the reason why it's empty is that you probably have a blank line in your file. If it so, you should add empty check like if (fullName.isEmpty()) continue; to iterate over the remaining lines.

Upvotes: 1

Dylanrr
Dylanrr

Reputation: 31

Your StringIndexOutOfBoundsException may be due to the data your receiving as we do not know the "Full Name". If someone doesn't have a first/middle/last name you will get the exception because you are not checking for this before the first middle and last are initialized. Move your initializers into your if statements and see if that helps.
Try this.

while(input.hasNextLine()){
   fullName = input.nextLine();
   if(fullName.isEmpty())
             fullName = input.nextLine();
   //Testing to see if the full name contains a middle name
   if(fullName.indexOf(" ") == fullName.lastIndexOf(" ")){
      first = fullName.substring(fullName.indexOf(" ")+1, fullName.indexOf(" ")+2);
      last = fullName.substring(0,1);
      initials = first + ". " + last + ".";
   }
   else{
      first = fullName.substring(fullName.indexOf(" ")+1, fullName.indexOf(" ")+2);
      middle = fullName.substring(fullName.lastIndexOf(" ")+1, fullName.lastIndexOf(" ")+2);
      last = fullName.substring(0,1);
      initials = first + ". " + middle + ". " + last + ".";
   }
   if(input.hasNextLine()){
      System.out.println(fullName + " yields " + initials);
   }  
}

Upvotes: 1

Related Questions