-2

The array is declared with a counter. The counter needs to be used each time an object is added into the array.

// declare Parcel array and parcel count variable
   private static Parcel[] parcels = new Parcel[10];
   private static int parcelCount = 0;

The information in the scanner needs to create a new Parcel object and store it in the next available (empty) spot in the array parcels.

Heres what Ive got. It doesn't seem to be working. What am I doing wrong?

System.out.print("Enter Parcel Number: ");
String parcelNumber = sc.nextLine();

String [] parcels = new String[0];

parcelCount ++;


System.out.print("Enter Sender Name: ");
String senderName = sc.nextLine();

String [] parcels = new String[1];

parcelCount ++;

System.out.print("Enter Return Address: ");
String returnAddress = sc.nextLine();

String [] parcels = new String[2];

parcelCount ++;

System.out.print("Enter Recipient Name: ");
String recipientName = sc.nextLine();

String [] parcels = new String[3];

parcelCount ++;

System.out.print("Enter Delivery Address: ");
String deliveryAddress = sc.nextLine();

String [] parcels = new String[4];

parcelCount ++;
  • 1
    Have you considered reading a tutorial about how arrays work: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html – OH GOD SPIDERS Oct 24 '17 at 10:11
  • so ... while showing us a piece of code in which you create quite the few new objects, you want us to tell you how to do that? – Stultuske Oct 24 '17 at 10:12
  • 1
    Why are you recreating `parcels` at all? Why don't you create it just at the end? And how are names and addresses related to the parcel count at all? – Thomas Oct 24 '17 at 10:17
  • Parcels is the array object of string. Why using it again? Unless you are going to place them within separate methods. – Kenneth Lhv Oct 24 '17 at 10:28
  • The parcelCount variable not only helps keep track of how many parcels are currently stored in the array, it also be used to help identify where the next available (empty) spot in the array is located. – Josh Gomez Oct 24 '17 at 10:39
  • This is super basic stuff. You can't just create a **new** array and assume that content that you pushed into **another** array magically appears in the new array. Each time you call `new` a new empty array is created. This is super basic stuff and explained in any good book or tutorial. So: please understand that this is not programming school were people *teach* you stuff that is written any newbie book out there. We help you making your steps on the way - but we don't do that first step for you that says "open a book and start reading" - instead of inventing your ideas on java semantics. – GhostCat Oct 24 '17 at 10:46

1 Answers1

0

Maybe you need https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html :)

Or use something like:

class ParcelHelper {
    static long count = 0;
    static Parcel[] parcels = new Parcel[10]

    public static void addParcels(Parcel parcel) {
        parcels[count] = parcel;
        count ++;
    }
}