0

Ive been trying to use a for loop to add instances of a driver to a driver array. Each driver has 3 basic variables that are gathered through the for loop. When the loop runs though, the details of the last driver are stored in all of the indexes of the array! I want to get it so that i can add each individual driver to the array.

public static void addDriver(Driver[] d) {      //method using for loop to add drivers


    for(int i = 0; i < d.length; i++ ) {


        String name, DOB, occupation;

        System.out.println("Please Enter Driver Name");
        name = kb.nextLine();

        System.out.println("Please Select Driver Occupation");
        System.out.println("1: Chauffeur" + "\n2: Accountant");
        int choice = kb.nextInt();
        kb.nextLine();

        if (choice == 1) {
            occupation = "Chauffeur";
        } else {
            occupation = "Accountant";
        }

        System.out.println("Please Enter Driver D.O.B");
        DOB = kb.nextLine();

        d[i]  = new Driver(name, occupation, DOB);

    }
}

any and all help greatly appreciated!

edit...

here is the code from the main method, i get the size of the array from a separate method called driverNum.

public static void main(String[] args) {

    int drivers = driverNum();      //Setting size of the array

    Driver[] d = new Driver[drivers];       //creating new array using number of drivers to be insured

    addDriver(d);       //calling method to add drivers to array

    for(int x = 0; x < d.length; x++)
    {
        System.out.println(d[x].toString());
    }

}

here is the Driver class that i have been using...

public class Driver {

static String name, occupation, DOB;

public Driver()
{
    name = "";
    occupation = "";
    DOB = "";
}

public Driver(String name, String occupation, String DOB)
{
    this.name = name;
    this.occupation = occupation;
    this.DOB = DOB;
}

public void setName(String name)
{
    this.name = name;
}

public String getName(Driver d)
{
    return name;
}

public void setOccupation(String occupation)
{
    this.occupation = occupation;
}

public String getOccupation()
{
    return occupation;
}

public void setDOB(String DOB)
{
    this.DOB = DOB;
}

public String getDOB()
{
    return DOB;
}

public String toString()
{
    String s;

    s = "Name: " + name;
    s = s + "\nOccupation: " + occupation;
    s = s + "\nDOB: " + DOB;

    return s;
}
}

Ive been scratching my head over this for a while now, because i thought it was correct. Thanks for the help so far!

4
  • What is the value of d.length before your loop? Commented Jun 28, 2017 at 19:23
  • Unless I'm missing something, this seems like it should work. Can you post the code where you are calling this method and printing out the results of it? Commented Jun 28, 2017 at 19:23
  • I don't think there are any mistakes here. As @JamalH said, post the code where you are calling this method, that might help. Commented Jun 28, 2017 at 19:25
  • Also add the Driver class definition please. Commented Jun 28, 2017 at 19:25

1 Answer 1

1

In your Driver class, you defined your three global variables, name, occupation, and D.O.B as static. This means that whenever you change the value of that variable, it will change everywhere in the program, even if you create multiple instances of that class. Just take out the static declaration and that should solve your problem.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.