0

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;

public class Main {

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 {

    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 {

    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 {

  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;

  }

}

  • 3
    The error messages tells you are trying to access the second element in an array that only has 1 element. Where exactly that happens however you didn't tell us. Also: We cannot "Fix this" as how many elements your arrays have literally depends on what input you are giving to your program, and you also didn't tell us what you are inputting. – OH GOD SPIDERS Aug 02 '22 at 09:32
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Aug 05 '22 at 11:45

1 Answers1

0

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
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[1] points at the second word of the line on your input (arrays start at index 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.
Please also check out the link in the comments below your question

The Blind Hawk
  • 1,199
  • 8
  • 24