java - How can I use arrays in a loop and have multiple outputs -


my goal have program asks relevant information create invoice. if user wishes add product/service, program loop , array increment, output provide description , price products. when running code error when try add product description:

exception in thread "main" java.lang.arrayindexoutofboundsexception: 1

i hope explained problem clearly.

import cs1.keyboard;  public class invoice_obj{      // object creates 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 invoice number          system.out.print("enter date\t\t: ");         string date = keyboard.readstring(); //user inputs date of invoice          system.out.print("who invoice to? : ");         string recipient = keyboard.readstring(); //user inputs recipient of 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 service/product? (y/n) : ");         answer = keyboard.readchar();           }while(answer=='y' || answer=='y');      }  } 

you initializing string[] description = new string[descnum];

as per code have initialized descnum = 1 create array of size 1 , trying access description[descnum++] = keyboard.readstring(); i.e., out of bound initialized array

try changing description[descnum++] = keyboard.readstring(); description[0] = keyboard.readstring();

or increase size of array


Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -