-1

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');

    }

}
deviantxdes
  • 469
  • 5
  • 17
  • It's usually helpful to provide a stack trace in order to help debug the issue. The exception in this case does help us understand but the stack trace helps so much more. – Dale Sep 22 '16 at 15:09
  • Besides the index errors (`description[descnum++]` and `price[pricenum++]`), you create a new array each time. Probably not what you want. Consider using an [ArrayList](https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html). – 001 Sep 22 '16 at 15:09
  • You define an array with only one element, then you are trying to place more than one element in it with indices which do not exist. – PEF Sep 22 '16 at 15:10
  • i sugguest u use a [List](http://stackoverflow.com/questions/858572/how-to-make-a-new-list-in-java) for your scenario – deviantxdes Sep 22 '16 at 15:11
  • public class Invoice_Obj{ // This object creates an invoice public void invoice(){ char answer; ArrayList descnumList = new ArrayList() ; ArrayList pricenumList = new ArrayList(); ... do{ ... descnumList.add(Keyboard.readString()); ... pricenumList.add(Keyboard.readString()); System.out.print("Add another service/product? (Y/N) : "); answer = Keyboard.readChar(); }while(answer=='Y' || answer=='y'); } } – Abdul Rizwan Sep 22 '16 at 15:38
  • //to show your input you can use foreach //example for(String data:descnumList) System.out.print("Your input:"+data); – Abdul Rizwan Sep 22 '16 at 15:39

1 Answers1

0

You are Initializing String[] description = new String[descnum];

as per your code you have initialized descnum = 1 so it will create array of size one and you are trying to access description[descnum++] = Keyboard.readString(); i.e., out of bound for initialized array

try changing description[descnum++] = Keyboard.readString(); to description[0] = Keyboard.readString();

or increase the size of your array

Jayanth
  • 5,954
  • 3
  • 21
  • 38