![](/img/trans.png)
[英]ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
[英]How to fix thid error ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1?
我不知道問題出在哪里 我正在嘗試打印客戶詳細信息 我已經嘗試更改變量但它不起作用 問題似乎是什么? 我已經嘗試了很多東西,但仍然無法修復錯誤。
導入 java.util.Scanner;
公共 class 主要 {
public static void main(String args[]) throws Exception {
Scanner sc = new Scanner(System.in);
String s1[] = sc.nextLine().split(" ");
String s2[] = sc.nextLine().split(" ");
String s3[] = sc.nextLine().split(" ");
int id = Integer.parseInt(s1[0]);
String name = s1[1];
String area = s2[0];
String city = s2[1];
int day = Integer.parseInt(s3[0]);
int month = Integer.parseInt(s3[1]);
int year = Integer.parseInt(s3[2]);
SimpleDate date = new SimpleDate(day, month, year);
Address add = new Address(area, city);
Customer c = new Customer(id, name, add, date);
System.out.print(c);
}
} class 簡單日期 {
private int day;
private int month;
private int year;
SimpleDate(int day, int month ,int year) {
this.day = day;
this.month = month;
this.year = year;
validateDate(this);
}
//gettens
public int getDay() {
return this.day;
}
public int getMonth() {
return this.month;
}
public int getYear() {
return this.year;
}
//setters
public void setDate(int day, int month, int year) {
this.day = day;
this.month = month;
this.year = year;
}
public static boolean validateDate(SimpleDate d) {
int day = d.getDay();
int month = d.getMonth();
int year = d.getYear();
if (year < 2000) {
return false;
}
if (month > 12 || month < 1) {
return false;
}
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if (day < 1 || day >31)
return false;
break;
case 4:
case 6:
case 9:
case 11:
if (day < 1 || day > 30)
return false;
break;
case 2:
if (day < 1 | day > 28) {
return false;
}
break;
}
return true;
}
@Override
public String toString() {
return (day + "/" + month + "/" + year);
}
}
class 地址{
private String area;
private String city;
Address(String area, String city) {
this.area = area;
this.city = city;
}
//吸氣劑
public String getArea() {
return area;
}
public String getCity() {
return city;
}
//二傳手
public void setArea(String area) {
this.area = area;
}
public void setCity(String city) {
this.area = city;
}
@Override
public String toString() {
return (area + ", " + city);
}
}
class 客戶{
private int custID;
private String name;
private Address address;
private SimpleDate registrationDate;
Customer(int custID, String name, Address address, SimpleDate registrationDate) {
this.custID = custID;
this.name = name;
this.address = address;
if (!(SimpleDate.validateDate(registrationDate)))
this.registrationDate = null;
else
this.registrationDate = registrationDate;
}
//getters
public Address getAddress() {
return this.address;
}
public SimpleDate getRegistrationDate() {
return this.registrationDate;
}
//setters
public void setAddress(Address address) {
this.address = address;
}
public void setRegistrationDate(SimpleDate registrationDate) {
if (!(SimpleDate.validateDate(registrationDate))) {
this.registrationDate = null;
} else {
this.registrationDate = registrationDate;
}
}
@Override
public String toString() {
String date = "";
if (this.registrationDate == null)
date = "unkown";
else
date = this.registrationDate.toString();
String add = "";
if (this.address == null)
add = "Unkown";
else {
add = this.address.toString();
}
String s = String.format("Id: %d\n" +" Name: %s\n" + "Address : %s\n" + "Registere: %d\n");
return s;
}
}
請提供更多細節。
究竟是哪條線導致了這個問題?
您也可以刪除除公共 static void 之外的所有代碼,因為它從未被調用
可以給出錯誤的兩個地方是這兩個:
String name = s1[1];
String city = s2[1];
問題是你給他們的輸入。 s1[1]
和s2[2]
指向輸入行的第二個單詞(數組從索引 0 開始)。 因此,要解決您的錯誤,您的輸入必須如下所示:
name name
area
city city
而且您可能只是以錯誤的方式編寫輸入。
或者,也許您打算這樣寫:
String name = s1[0];
String city = s2[0];
在必須在stackoverflow上發布問題之前,有很多地方解釋了索引越界錯誤。
另請查看您問題下方評論中的鏈接
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.