0

I have an array list of Fleets (Each fleet will hold its own list of Trucks).

I have a fleet class with the constructor

public Fleet(String businessName){
    this.businessName = businessName;
    this.fleetList = new ArrayList<Truck>();
}

So:

In my TUI, I have a helper method called createFleet. When the user presses 1 on the menu, it asks for a name of his business, and then makes a fleet named that. That method is:

public static void createFleet(){
    System.out.println("");
    System.out.println("Please enter the name of the fleet.");
    inputText = scan.nextLine();

    fleetCollection.add(new Fleet(inputText));
    printFleets();
    System.out.println("");
    System.out.println("--- Fleet: " + inputText + " added ---");
    System.out.println("");
}

And my problem is that when I add one Fleet, and print the results I get:

Fleet 0: Fleet Number One

But when I add Fleet Number One, then press 1 on the menu again to add ANOTHER fleet (named Fleet Number Two) and print the fleet list, the results are:

Fleet 0: Fleet Number Two
Fleet 1: Fleet Number Two

It seems to be confusing the two...and this further breaks the program when I try to add trucks to the fleet, because It cannot pick the "right" fleet.

Please let me know if you need any other of my code. I just need this to correctly add and print the fleets in the fleet list:

private static ArrayList<Fleet> fleetCollection;

Thank you :) for all the help!

2
  • can you post code of printFleets() method? Commented Feb 28, 2012 at 6:37
  • 1
    Hey man.... where is your question?? Commented Feb 28, 2012 at 7:14

4 Answers 4

3

You probably declared businessName in Fleet class static, if so then remove it

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

4 Comments

In that case,getBusinessName() should be NOT static too
remove method modifier static of getBusinessName()
remove method modifier static of fleetList
use fleet.getBusinessName() instead of Fleet.getBusinessName(); // use fleet object, do not use as static
3

You need to be clear about use of static

static variable

  • It is a variable which belongs to the class and not to object(instance)
  • Static variables are initialized only once , at the start of the execution . These variables will be initialized first, before the initialization of any instance variables
  • A single copy to be shared by all instances of the class
  • A static variable can be accessed directly by the class name and doesn’t need any object
  • Syntax : <class-name>.<variable-name>

static method

  • It is a method which belongs to the class and not to the object(instance)
  • A static method can access only static data. It can not access non-static data (instance variables)
  • A static method can call only other static methods and can not call a non-static method from it.
  • A static method can be accessed directly by the class name and doesn’t need any object
  • Syntax : <class-name>.<method-name>
  • A static method cannot refer to "this" or "super" keywords in anyway

src : http://www.javatutorialhub.com/java-static-variable-methods.html

Comments

2

have you overridden the equals method in Fleet ? if so and it's not correct, it could be the cause of your weird result

2 Comments

you need an instance from that class to be able to call a non-static method; like this: Object a = new Object(); a.method();
Should I open a new question for that problem? Or should I erase everything above, and post the new question here?
1

I made small modifications to your program to make it simple.

class Fleet{
String businessName;
public Fleet(String businessName)
{ this.businessName = businessName;}
public String getBusinessName()
{
    return businessName;
}
}

public class T {
private static ArrayList<Fleet> fleetCollection = new ArrayList<Fleet>();

    public static void main(String[] args) 
    {
        createFleet("A");
         printFleets();
         createFleet("B");
        printFleets();
    }
     public static void createFleet(String name){
    System.out.println("");
    fleetCollection.add(new Fleet(name));
}
public static void printFleets(){
    Iterator i = fleetCollection.iterator();
    Fleet f;
    while(i.hasNext())
    {
        f = (Fleet)i.next();
        System.out.println(f.getBusinessName());
    }
}

}

It prints as expected.

A

A
B

Check your access modifiers on "businessName" field. It shouldn't be static. Also check printFleets() method.

1 Comment

Thank you :) It now returns the fleets properly but my addTruck() is still broken.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.