简体   繁体   English

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

[英]How to fix thid error ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1?

I don't know where the problem is occurring I am trying to print customer details I already tried to change the variables but it didn't work What seems to be the problem?我不知道问题出在哪里 我正在尝试打印客户详细信息 我已经尝试更改变量但它不起作用 问题似乎是什么? i already tried so many things but still not able to fix the errors.我已经尝试了很多东西,但仍然无法修复错误。

import java.util.Scanner;导入 java.util.Scanner;

public class Main {公共 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 SimpleDate { } 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 Address { class 地址{

    private String area;

    private String city;


    Address(String area, String city) {

        this.area = area;
        this.city = city;

    }

//getters //吸气剂

    public String getArea() {

        return area;

    }

    public String getCity() {

        return city;

    }

//setters //二传手

    public void setArea(String area) {

        this.area = area;

    }

    public void setCity(String city) {

        this.area = city;
    }

    @Override

    public String toString() {

        return (area + ", " + city);

    }

}

class Customer { 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;

  }

} }

Please give out more details.请提供更多细节。
What line causes this issue exactly?究竟是哪条线导致了这个问题?
Also you can remove all the code aside from the public static void since its never called您也可以删除除公共 static void 之外的所有代码,因为它从未被调用
The two places that can be giving out the error are these two:可以给出错误的两个地方是这两个:

String name = s1[1];
String city = s2[1];

And the issue is the input you are giving them.问题是你给他们的输入。 s1[1] and s2[2] points at the second word of the line on your input (arrays start at index 0). s1[1]s2[2]指向输入行的第二个单词(数组从索引 0 开始)。 So to solve your error your input would have to look like this:因此,要解决您的错误,您的输入必须如下所示:

name name
area
city city

And you are probably just writing the input the wrong way.而且您可能只是以错误的方式编写输入。
Or maybe you meant to write this:或者,也许您打算这样写:

String name = s1[0];
String city = s2[0];

There are many places explaining index out of bounds error before having to post a question on stackoverflow.在必须在stackoverflow上发布问题之前,有很多地方解释了索引越界错误。
Please also check out the link in the comments below your question另请查看您问题下方评论中的链接

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ArrayIndexOutOfBoundsException:索引 3 超出长度 3 的范围 - ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3 如何在 Spring 引导中修复“嵌套异常是 java.lang.ArrayIndexOutOfBoundsException:索引 2 超出长度 2 的范围” - ZE4728F444B24839E3F80ADF3829BCBA - How to fix "nested exception is java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 2" in a Spring Boot - postgresql Project 如何修复 Java 中的“Index 5 out of bounds for length 5”错误 - How to fix “Index 5 out of bounds for length 5” error in Java ArrayIndexOutOfBoundsException:索引 30 超出长度 30 的范围 - ArrayIndexOutOfBoundsException: Index 30 out of bounds for length 30 线程“主”java.lang.ArrayIndexOutOfBoundsException 中的异常:索引 1 超出长度 0 的范围,不知道这是什么意思或如何解决 - Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 0, no idea what does that mean or how to fix it 我如何解决 ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1 当尝试单独打印一个 csv 文件时 - How can i solve ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1 when trying to print out a csv file individually 如何修复数组索引超出范围的错误? - How to fix array index out of bounds error? java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1 正在接收错误,无法确认原因 - java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1 error is being received and cannot confirm why java.lang.ArrayIndexOutOfBoundsException:在二进制搜索中,长度为 7 的索引 -1 越界 - java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 7 on a binary search 如何解决越界索引错误? - How to fix out of bounds index error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM