繁体   English   中英

如何修复错误ArrayIndexOutOfBoundsException:索引1超出长度1的范围?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM