Hello,

I'm having trouble with the ArrayList add method it seems to be giving me an error in BlueJ. It say's no sutiable method found for add(int) method java.util.ArrayList.add(int,Order)... And how would I create a new object inside a method which will add the parameters to the arraylist.

Thanks

Here's my code:

public class OrderBook
{

    ArrayList<Order> Orders;


    public OrderBook()
    {
      Orders = new ArrayList<Order>();
    }


    public void addNewOrder(String name, int quantity)
    {


        new OrderBook(); 
        Orders.add(name);
        Orders.add();
    }

Recommended Answers

All 11 Replies

You have defined the ArrayList as an ArrayList of Orders. That means the only thing you can add to that list is an Order. You can't add a String (line 18) and you can't add nothing (line 19).
You didn't post the code for the Order class, but presumably it has a suitable constructor and set method(s) so you can create a new Order with the required info, then you can add that new Order to the ArrayList.

A question: Why do you create an Orderbook object on line 17? If you don't assign what is returned by the new, the object will not be avaiable for use anywhere. There must already be an object existing for the addNewOrder()method to be called.

Hi,

Do you mean this?

Order.add(name);
I still tried that, but it didn't seem to work. Could you show me an example for this please?

Thanks

Hi,

This the full code.

import java.util.ArrayList; 

public class OrderBook
{

    ArrayList<Order> Orders;// DECLARE ArrayList<Order> FIELD HERE


    public OrderBook()
    {
      Orders = new ArrayList<Order>();  
    }


    public void addNewOrder(String name, int quantity)
    {


        new OrderBook();
        Order.add();
        Order.add();
    }


    public int getOrderCount()
    {
        return Orders.size(); 
    }


    public void printOrders()
    {
       // for(String filename :Orders){
        //System.out.println("Name: " + order.getName());    


    }


    public int orderTotal()
    {

    }


    public int orderTotalForCustomer(String name)
    {

    }


    public Order lookUpOrder(String name)
    {

    }
}

it didn't seem to work

Could you explain what that means?
If you get errors, copy the full text of the error messages and paste them here.

an example

ArrayList<String> alS = new ArrayList<String>();   //  define an ArrayList that holds Strings

alS.add("A String");    //  add a String to the ArrayList

alS.add(new Point()); // this will give an error because alS is defined to hold Strings

That code refers to a class called Order. Where's the code for that class?

Sorry, I didn't post the code for the Order earlier.

public class Order
{

    private String name;


    private int quantity;


    public Order(String name, int quantity)
    {
        this.name = name;
        this.quantity = quantity;
    }


    public int getQuantity()
    {
        return quantity;
    }


    public String getName()
    {
        return name;
    }
}

OK, that's excellent. Like I said before, you create a new Order with the required info (name, quantity), then you can add that new Order to the ArrayList.

Thanks, that helped me, but for some reason it's giving me an error message saying cannot find symbol - method.add()

  public void addNewOrder(String name, int quantity)
    {
       Order Orders;  
       Orders = new Order(name,quantity);

       Order.add();
       Order.add();



    }

Of course it is. What are you adding? Check out the API documentation to see what parameter(s) you need for ArrayList's add method.
You also need to sort out your variable names - they should all begin with a lower case letter and they should all be unique.

Thank you, it's working now and it's not throwing me any errors.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.