My goal is to have a program which asks for all the relevant information to create an invoice. If the user wishes to add another product/service, the program will loop and the array will increment, so that the output will provide a description and price for all the products. When running this code i get this error when i try to add a product description:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
I hope I explained my problem clearly.
import cs1.Keyboard;
public class Invoice_Obj{
// This object creates an invoice
public void invoice(){
char answer;
int descnum = 1;
int pricenum = 1;
System.out.print("Enter invoice #\t\t: ");
String invoicenum = Keyboard.readString(); //user inputs the invoice number
System.out.print("Enter Date\t\t: ");
String date = Keyboard.readString(); //user inputs the date of the invoice
System.out.print("Who is this invoice to? : ");
String recipient = Keyboard.readString(); //user inputs the recipient of the invoice
do{
System.out.print("\nDescription of service or product : ");
String[] description = new String[descnum];
description[descnum++] = Keyboard.readString();
System.out.print("\nTotal price of service or product : ");
String[] price = new String[pricenum];
price[pricenum++] = Keyboard.readString();
System.out.print("Add another service/product? (Y/N) : ");
answer = Keyboard.readChar();
}while(answer=='Y' || answer=='y');
}
}