tags:

views:

117

answers:

4

I am trying to turn my array into an arraylist and I am having trouble modifying this part of my class. I don't have my complete class but this part of it.

   public boolean add(String title, int productID){//true or false statement

        if ( numberOfRecords == list.length() ){  // I get an error here at list.length()
    return false;

}
    else
    { list [numberOfRecords] = new MyArrayList(title, productID);// I get an error
    numberOfRecords++;
    return true;
    }

}
+3  A: 

It's impossible to tell from the snippet you've provided. There are too many variables that are unknown. But Paul Tomblin is correct: List doesn't have a length() method, but array has a length attribute. Which one describes your list variable?

Compiler issues should almost NEVER be part of a question here. When the compiler gives you a message, it's likely to give you enough information to figure it out with a little research.

When you post a question asking about compilation issues, it says that you weren't willing to invest enough energy or effort to figure it out for yourself.

This works properly. See how your code compares:

package movie;

/**
 * ShoppingCart
 * User: Michael
 * Date: 10/16/10
 * Time: 8:07 PM
 */
public class ShoppingCart
{
    private Movie[] list;
    private int numberOfRecords;

    public static void main(String[] args)
    {
        ShoppingCart cart = new ShoppingCart(args.length);
        for (int i = 0; i < args.length; ++i)
        {
            boolean success = cart.add(args[i], i);
            System.out.println("Movie '" + args[i] + (success ? "' was " : " was not ") + "added successfully");
        }
        boolean success = cart.add("Gladiator", args.length+1);
        System.out.println("Movie 'Gladiator'" + (success ? " was " : " was not ") + "added successfully");

        System.out.println(cart);
    }

    public ShoppingCart(int numberOfRecords)
    {
        this.numberOfRecords = 0;
        this.list = new Movie[numberOfRecords];
     }

    @Override
    public String toString()
    {
        StringBuilder builder = new StringBuilder();

        String newline = System.getProperty("line.separator");
        for (Movie movie : this.list)
        {
            builder.append(movie).append(newline);
        }

        return builder.toString();
    }

    public boolean add(String title, int productId)
    {
        if (numberOfRecords == list.length)
        {
            return false;
        }
        else
        {
            list[numberOfRecords] = new Movie(productId, title);
            numberOfRecords++;
            return true;
        }


    }
}

class Movie
{
    private int id;
    private String title;

    Movie(int id, String title)
    {
        this.id = id;
        this.title = title;
    }

    public String getTitle()
    {
        return title;
    }

    @Override
    public String toString()
    {
        return "Movie{" +
               "id=" + id +
               ", title='" + title + '\'' +
               '}';
    }
}
duffymo
Well I'm sorry for not knowing java but I have been going around and around with this project so you cannot say I have not worked hard enough. I'm new to java and I have been working with this for the past week.
Lola
I'm just saying that when the compiler tells you something is wrong with your code it tends to be specific. It tells you the class, the line, the problem, etc. You have plenty of information to get started. Then it's time to start scouring the javadocs to see what you might have to correct. You're quite welcome for the code. I love giving my time to people like you.
duffymo
Well thank you so much for the code but I already have that. I just don't know how to describe java very well. I just started learning this stuff. I don't know if you would like to see what I have worked on but since I don't know how to work this site I can't. But I'm sorry for being disrespectful but this project has me going crazy. I have been working on this for the past two weeks and I'm just having trouble.
Lola
Wait a second - you had code, but it didn't compile or run. Mine does both. You're obviously a student with a long way to go. One of the most important lessons you'll have to learn if you manage to stay in this field and do this for a living is how to manage your emotions. The kind of frustration you're showing here will kill you over the course of a long project. Believe me, fixing compilation errors will be the least of your worries. I'm saying that these kinds of issues should not be tying you up for two weeks.
duffymo
Its actually several classes I posted them before on one of my questions. It has all the classes that I'm working with. Its the main class, inventory class, movie class, and menu class, and I have to make an arraylist class.
Lola
And I'm supposed to know that by breathlessly following all your questions? Five classes in two weeks - just about three days per class. How many of those were compiling and running successfully before you posted here? How many are compiling and running right now? I will say one thing: you'd better accept one of these answers if it got you past this error. You get help here by the grace of strangers who donate their time. Having a reputation for accepting answers and giving credit where it's due will help your future efforts.
duffymo
No I was not saying for you to go and see my other questions. I was letting you know how many classes I have been working on. Those 4 classes were given to us by the professor and we are suppose to add a new class which is an arraylist but we are suppose to modify the other classes. You have made me feel bad about myself I feel dumb and stupid now. I think I might as well just drop the course since I don't think I could make it.
Lola
Lola, everyone learning a new skill has to get over that uncomfortable feeling of being ignorant - including me and Jon Skeet. Nobody is born knowing how to be good at something. You either figure out how to deal with that discomfort or move onto something else. You should give up if a minor criticism from someone like me about not researching compiler errors or an assignment to add one new class and modify four others makes you feel so bad. Or maybe you should tell me to screw myself, press on, and endure the pain required to get an A. But only you can make that decision. Good luck.
duffymo
I just think I am not good for this. Just by your comment it made me think that I stress over this course alot and I shouldn't. I very good with math and computers but not computer science. So I'm thinking of changing my degree. Good luck yourself
Lola
It's hard to see how you can make an objective decision without having completed even a single class. For those things you claim to be good at - how long did it take to feel comfortable? Or are you just a natural? If I were you, I'd stick out at least one class, endure the stress, and make a decision at the end. The important lesson you might learn is how to deal with the stress and stick out things that are hard. You're missing an opportunity if you quit.
duffymo
+1  A: 

to call list [numberOfRecords], list must be an array instance.

Hovewer,

if ( numberOfRecords == list.length() ){  // I get an error here at list.length()
    return false;
}

this section of your code says that list is an java.util.List implementation instance.

You should modify

list [numberOfRecords] = new Movie(title, productID);

this section to:

list.add(new Movie(title, productID));
feridcelik
+1  A: 

To use a List in place of an array you'll have to make the following changes:

list.length() to list.size()

list [numberOfRecords] list.add(new Movie(title, productID))

codaddict
+1  A: 

It seems that you are implementing your own ArrayList. So list is the array and numberOfRecords is the number of elements that are present and filled-out in the array.

If that's correct, you should not call your array list, since the name is misleading. A better name would be elements.

And, as others have noted, arrays don't have a method length, it's a field instead. So you should write elements.length instead of elements.length().

Roland Illig