I am writing a program to display Dewey Decimal Information. I don't have much yet, but am already getting the following error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at DDC.main(DDC.java:19)
I don't understand why the error is array related
here's my code:
// enter a book's catalog number and the program produces a
// list of the information the catalog number provides for the user
import java.util.Scanner;
public class DDC {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Prompt the user to enter a catalog number
System.out.println("Enter the catalog number: ");
String catalognumber = input.nextLine();
int strLength = catalognumber.length();
if (strLength == 15) {
String[] parts = catalognumber.split(" ");
String topicarea = parts[0];
String authorID = parts[1];
String titleID = parts[3];
System.out.println("This is a Dewey Decimal Classification(DDC) for Non-fiction");
System.out.println("Topic Area: "+ topicarea);
System.out.println("Author Identifier: "+ authorID);
System.out.println("Title Identifier: " + titleID);
System.out.println("Thanks for using the Catalog Information Program.");
} // end if
} // end main
} // end DDC
String[] parts. What size is this array? What positions are you trying to access?catalognumber? Line 19 appears to beString titleID = parts[3];. Perhaps you meantparts[2]here?