-1
   public static void main(String[] args) {
    int i;
    ArrayList apartment = new ArrayList();
    ArrayList area = new ArrayList();
    ArrayList lightbulb = new ArrayList();
    ArrayList windows = new ArrayList();
    System.out.println("Введите количество квартир в здании.");
    Scanner sc = new Scanner(System.in);
    int house = sc.nextInt();
    apartment.add("");
    area.add("");
    lightbulb.add("");
    windows.add("");
    for (i=1; i<=house; i++){
        Apartments home = new Apartments();
        System.out.println("Введите площадь квартиры " + i);
        Scanner S = new Scanner(System.in);
        home.area = S.nextDouble();
        area.add(home.area);
        System.out.println("Введите колич1ество окон в квартире " + i);
        Scanner W = new Scanner(System.in);
        home.windows = W.nextInt();
        windows.add(home.windows);
        System.out.println("Введите количество лампочек в квартире " + i);
        Scanner L = new Scanner(System.in);
        home.lightbulb = L.nextInt();
        lightbulb.add(home.lightbulb);
    }
    for(i=1; i<=house; i++)
    System.out.println("Площадь квартиры " + i + "составляет " + area.get(i));
    System.out.println("Количество окон в квартире равно: "  + windows.get(i));
    System.out.println("Количество лампочек в квартире равно:  " + lightbulb.get(i));
}

Hello everebody! I cant understand where is a mistake in this code. Im getting this error:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
    at java.util.ArrayList.rangeCheck(ArrayList.java:657)
    at java.util.ArrayList.get(ArrayList.java:433)
    at Planning.main(Planning.java:37)

IDEA showing that mistake somewhere in this part System.out.println("Количество окон в квартире равно: " + windows.get(i)); but still i cant understand whats wrong

2 Answers2

2

you forgot the curled brackets around your println-statements:

for(i=1; i<=house; i++)
    System.out.println("Площадь квартиры " + i + "составляет " + area.get(i));
    System.out.println("Количество окон в квартире равно: "  + windows.get(i));
    System.out.println("Количество лампочек в квартире равно:  " + lightbulb.get(i));

Because of this the second and third println-statemt run AFTER the for loop, and i is now house+1, which leads to the out of range exception.

wech
  • 91
  • 5
1

For one, at the bottom there with your final print statements, you're only iterating over the first print statement since you don't have any brackets with that for loop. I would recommend using a scoped variable in the loop so it will tell you it's not declared if you try to use it outside of the scope of the for loop, it can avoid confusion like this

for(int i=1; i<=house; i++) {
  ...
}

Second, adding "" to every list seems incredibly redundant, so you probably should remove that. Array lists are zero-indexed, so the first index is accessed with list.get(0), this will probably eliminate the need for these additions. If you still want the user to input 1, then you can just subtract 1 when accessing: list.get(house - 1).

Third, if you do remove those unneeded adds, you can loop through the list starting at 0,

for(i=0; i<house; i++) {
    System.out.println("Площадь квартиры " + i + "составляет " + area.get(i));
    System.out.println("Количество окон в квартире равно: "  + windows.get(i));
    System.out.println("Количество лампочек в квартире равно:  " + lightbulb.get(i));
}

Finally, these multiple lists seem quite redundant since you're storing the same information in the home variable each time. I'm assuming the ArrayList apartment is intended to store these, though you're not using it. So, instead of all of these separate lists, just store home in the apartment list and access it's fields when you need them. This is much more efficient also since you can fetch the home once since getting from a list is a relatively expensive operation, and accessing it's fields will be significantly more efficient. Also, you can just use the same scanner.

public static void main(String[] args) {
    ArrayList apartment = new ArrayList();
    System.out.println("Введите количество квартир в здании.");
    Scanner sc = new Scanner(System.in);
    int house = sc.nextInt();
    for (int i=1; i<=house; i++) {
        Apartments home = new Apartments();
        System.out.println("Введите площадь квартиры " + i);
        home.area = sc.nextDouble();
        System.out.println("Введите колич1ество окон в квартире " + i);
        home.windows = sc.nextInt();
        System.out.println("Введите количество лампочек в квартире " + i);
        home.lightbulb = sc.nextInt();
        apartment.add(home)
    }
    for(int i=0; i<house; i++) {
        Apartments home = apartment.get(i);
        System.out.println("Площадь квартиры " + i + "составляет " + home.area);
        System.out.println("Количество окон в квартире равно: "  + home.windows);
        System.out.println("Количество лампочек в квартире равно:  " + home.lightbulb);
    }
}
obermillerk
  • 1,560
  • 2
  • 11
  • 12