0

When I try to run, I enter the daily fine and I get this error "java.lang.stringindexoutofboundsexception string index out of range: 10 (in java.lang.String)" I do not know why.

import java.util.Scanner;
    public class Fines 
        {
            public static void main(String[] args) 
                {
                    //Makes the Scanner Object
                    Scanner in = new Scanner(System.in);
                    //Decoration
                    String decor = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
                    //Integers
                    int chkMonthInt;
                    int chkDayInt;
                    int chkYearInt;
                    int lateMonthInt;
                    int lateDayInt;
                    int lateYearInt;
                    int monthsLate;
                    int daysLate;
                    int yearsLate;
                    double rateDouble;
                    //Obtaining Information
                    System.out.print("Enter Last Name: ");
                    String Last = in.next();
                    System.out.print("Enter First Name: ");
                    String First = in.next();
                    System.out.print("Enter SSN: ");
                    String SSN = in.next();
                    System.out.print("Enter the title of the book: ");
                    String TitleA1 = in.next();
                    String TitleA2 = in.nextLine();
                    System.out.print("Enter the date checked out (MM/DD/YYYY): ");
                    String Date = in.next();
                    System.out.print("Enter current date (MM/DD/YYYY): ");
                    String Due = in.next();
                    System.out.print("Enter daily fine rate: ");
                    String Rate = in.next();
                    //Stringing Checkout dates
                    String chkMonth = Date.substring(0, 2);
                    String chkDay = Date.substring(3, 5);
                    String chkYear = Date.substring(6, 10);
                    //Stringing Due dates
                    String lateMonth = Due.substring(0, 2);
                    String lateDay = Due.substring(3, 5);
                    String lateYear = Due.substring(6, 10);
                    //Parsing
                    chkMonthInt = Integer.parseInt(chkMonth);
                    chkDayInt = Integer.parseInt(chkDay);
                    chkYearInt = Integer.parseInt(chkYear);
                    lateMonthInt= Integer.parseInt(lateMonth);
                    lateDayInt = Integer.parseInt(lateDay);
                    lateYearInt = Integer.parseInt(lateYear);
                    rateDouble = Double.parseDouble(Rate);
                    //Calculations
                    monthsLate = (lateMonthInt - chkMonthInt);
                    daysLate = (lateDayInt - chkDayInt);
                    yearsLate = (lateYearInt - chkYearInt);
                    //Print out the information
                    System.out.println("\n");
                    System.out.println("To: " + Last + ", " + First + "\t\tAccount: " + Last + First.substring(0, 3) + SSN.substring(6, 10));
                    System.out.println("From: Librarian");
                    System.out.println("Subject: Overdue Notice");
                    System.out.println(decor);
                    System.out.println(TitleA1 + TitleA2 + " was checked out on: " + Date);
                    System.out.println("This book is currently " + monthsLate + "/" + daysLate + "/" + yearsLate + " days late.");
                    System.out.println("Your fine has accumulated to: $" + (daysLate * rateDouble));


                }

        }
kosa
  • 65,990
  • 13
  • 130
  • 167
bhsingh
  • 101
  • 1
  • 8
  • 14

4 Answers4

4

It means you're trying to reference an array index that is greater than the actual array size.

jordaniac89
  • 574
  • 2
  • 8
  • 27
1

Java is zero indexed. Say your social is 123456789 that is 0-8, so calling 6-10 won't work, if you want the last 4, it would be 5-9 (since last is non-inclusive). Also, you need to parse this, what if the user enters 123-45-6789?

You didn't actually show on which line you were getting this for however, but the above example shows the error in all of your - to the tenth substring, methods.

A way to the above question would be as simple as

SSN.substring(SSN.length() - 4, SSN.length())
Dispersia
  • 1,436
  • 9
  • 23
  • His `substring` method calls are correctly. The stored string is just too short. – Tom Aug 11 '14 at 18:58
  • My comment was targeting that he is going to the tenth using substring, which is incorrect (because as you said, the string is to short), not that the method call was. – Dispersia Aug 11 '14 at 19:00
  • And why do you mention the entered social number if he get's the error after entering the daily fine (as he mentioned)? The next thing that happens is the "splitting" of the entered date. – Tom Aug 11 '14 at 19:05
0

One (or both) of String Date or String Due is not 10 characters long,

String chkMonth = Date.substring(0, 2);
String chkDay = Date.substring(3, 5);
String chkYear = Date.substring(6, 10); // <-- Here
//Stringing Due dates
String lateMonth = Due.substring(0, 2);
String lateDay = Due.substring(3, 5);
String lateYear = Due.substring(6, 10); // <-- and/or Here

The easiest solution I can see is to add a check.

 String chkYear = (Date.length() >= 10) ? Date.substring(6, 10) : "";
 // ...
 String lateYear = (Due.length() >= 10) ? Due.substring(6, 10) : "";

Also, Java naming conventions would be String date and String due. Your way Date and Due look like class names.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

You are sure the Date and Due strings are correctly filled in? It could be that you are doing trying to get a substring from a string that isn't that long. :)

More info can be found here: http://docs.oracle.com/javase/7/docs/api/java/lang/StringIndexOutOfBoundsException.html

or here

Java substring : string index out of range

Community
  • 1
  • 1
ThijsM
  • 200
  • 1
  • 14