当我尝试跑步时,我输入每日罚款,我收到此错误“java.lang.stringindexoutofboundsexception string index out of range: 10 (in java.lang.String)"
我不知道为什么。
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));
}
}
答案 0 :(得分:4)
这意味着您正在尝试引用大于实际数组大小的数组索引。
答案 1 :(得分:1)
Java是零索引的。假设您的社交是123456789,即0-8,所以呼叫6-10不会工作,如果你想要最后4,它将是5-9(因为最后是非包容性的)。此外,您需要解析此问题,如果用户输入123-45-6789,该怎么办?
但是,您实际上并没有显示您获得此项的哪一行,但上面的示例显示了您的所有 - 第十个子字符串方法中的错误。
解决上述问题的方法就像
一样简单SSN.substring(SSN.length() - 4, SSN.length())
答案 2 :(得分:0)
String Date
或String Due
中的一个(或两个)长度不是10个字符,
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
我能看到的最简单的解决方案是添加支票。
String chkYear = (Date.length() >= 10) ? Date.substring(6, 10) : "";
// ...
String lateYear = (Due.length() >= 10) ? Due.substring(6, 10) : "";
此外,Java命名约定为String date
和String due
。您的Date
和Due
类似于类名。
答案 3 :(得分:0)
您确定正确填写了日期和截止字符串吗?可能是你正在尝试从不长的字符串中获取子字符串。 :)
可以在此处找到更多信息:http://docs.oracle.com/javase/7/docs/api/java/lang/StringIndexOutOfBoundsException.html
或在这里