Hello
I have this code:
When I am running the program and i am making records, i have no problem entering information about the object with index 0. When i enter information about object with index 1 i get this error:import java.io.BufferedWriter; import java.io.FileWriter; import java.util.Scanner; import java.util.ArrayList; class Specification { String name; String IP; String OS; int RAM; } ... //adding information public void AddInfo(String n, String f, String o, int r){ setName(n); setIP(f); setOS(o); setRAM(r); System.out.println("Record done!"); } ... public static void main(String[] args) { int i; Scanner scan=new Scanner(System.in); ArrayList<Specification> a = new ArrayList<Specification>(); a.add(new Specification()); while(true){ System.out.println("Choose your option"); System.out.println("1. Add information"); System.out.println("2. Print the entered information"); System.out.println("3. Write to file"); System.out.println("4. Search for an item"); System.out.println("Enter a number dofferent from those from above (ex.5)"); int choice1=scan.nextInt(); if(choice1>4)break; switch (choice1) { case 1: { System.out.println("How many records do you want to make?"); int enterings; enterings=scan.nextInt(); for(i=0;i<enterings;i++) { System.out.println("Enter name"); String entered_name=scan.next(); System.out.println("Enter IP"); String entered_IP=scan.next(); System.out.println("Enter OS"); String entered_OS=scan.next(); System.out.println("Enter RAM"); int entered_RAM=scan.nextInt(); a.get(i).AddInfo(entered_name, entered_IP, entered_OS, entered_RAM); } } break; ... } } }
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.ArrayList.RangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at Specification.main(Specification.java:178)
My aim is to make several records to the ArrayList