Java - Trouble injecting user input into empty array (text-based app for registering products)

Viewed 30

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); 
  }  
}
2 Answers

Arrays always need a predetermined length. Instead you could use an ArrayList which can grow. Consider the following code example:

List<Integer> list = new ArrayList<Integer>();
list.append(42);
list.append(84);
// The list now contains: [42, 82]
list.get(0); // Returns 42

You are initializing String[] items = {}; double[] prices = {}; with no elements i.e. with capacity of 0. Because arrays are fixed sized.

So you if you know the max size of the array which seems to be 10 from your code then initialize like below:

String[] items = new String[10];
double[] prices = new double[10];

And if you are uncertain about the max size then you can use ArrayList which starts from index 0 and it accepts only objects. So, you need to use wrapper classes i.e. for example for int use ArrayList<Integer>

You can read more ArrayList

Related