Reputation: 101
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));
}
}
Upvotes: 0
Views: 154
Reputation: 1438
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())
Upvotes: 1
Reputation: 200
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
Upvotes: 0
Reputation: 201527
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.
Upvotes: 0
Reputation: 574
It means you're trying to reference an array index that is greater than the actual array size.
Upvotes: 4