I'm trying to come up with a text-based app for registering and listing items using arrays, but I'm not being successful at creating an empty array and "injecting" information in it. Could you help me out? When I choose option 1 it brings the exception "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at AppTrabFinal.main(AppTrabFinal.java:38)". I know it is related to empty arrays, but my teacher didn't teach us how to build one without a pre-determined length and I'm lost trying to do it myself. Thanks in advance
import java.util.Scanner;
public class AppTrabFinal {
public static void main(String[] args) {
Scanner entrada = new Scanner(System.in);
int i, option;
String[] items = {};
double[] prices = {};
System.out.printf("Welcome to the Yellow Submarine store. What would you like to do?\n");
do {
System.out.println("\n========= Option Menu =========");
System.out.println("1 – Register a product");
System.out.println("2 – List your products");
System.out.println("3 – Exit");
System.out.println("==================================\n");
System.out.println("Choose an option: ");
option = entrada.nextInt();
switch (option) {
case 1:
for(i = 0; i < 10; i++) {
System.out.println("\nType the name of the product you would like o register: ");
items[i] = entrada.nextLine();
System.out.println("Enter it's price: ");
prices[i] = entrada.nextDouble();
}
break;
case 2:
for(i = 0; i > 0; i++) {
System.out.println(items[i+1]);
}
break;
case 3:
System.out.println("\nLogging out. Thank you for using our app.");
break;
default:
System.out.println("Invalid option, try a different number.");
break;
}
} while (option != 3);
}
}