Hi,

I've written the below code as part of a University assignment, it compiled ok in Eclipse but returns the <identifier> expected error when compiling via the command line. The error appears on lines 7, 8, 12, 13 and 20. Could anyone shed any light on why i'm getting it back?

And on a separate note, if anyone happens to know why the takeIn method on row 61 isn't unassigning a CD to a Person I'd love to know as I've no idea!

Thanks in advance.

import java.util.ArrayList;
import java.io.*;

public class CDStore implements Serializable{

    String name;
    ArrayList<CD> discsArray;
    ArrayList<Person> peopleArray;

    public CDStore(String name) {
        this.name = name;  
        discsArray = new ArrayList<CD>();
        peopleArray = new ArrayList<Person>();
    } 

    public String getName() {
        return name;
    } 

    public ArrayList<CD> getDiscs() {
        return discsArray;
    } 

    public ArrayList<Person> getPeople() {
        return peopleArray;
    } 

    public void addDisc(CD aCD) {
        this.discsArray.add(aCD);
    } 

    public void removeDisc(CD aCD) {
        this.discsArray.remove(aCD);
    }

    public void addPerson(Person aPerson) {
        this.peopleArray.add(aPerson);
    }

    public void removePerson(Person aPerson) {
        this.peopleArray.remove(aPerson);
    }

    public boolean takeOut(CD aCD, Person aPerson) {
        //int out = 0;
        int out;
        if((out = discsArray.indexOf(aCD)) != 0)
        {
            CD cd = (CD)discsArray.get(out);

            if(cd.person == null)
            {
                cd.person = aPerson;
                discsArray.set(out, cd);
                return true;
            }
        }
        return false;
    }

    public boolean takeIn(CD aCD, Person aPerson) {
        int in;
        if((in = discsArray.indexOf(aCD)) != 0)
        {
            CD cd = (CD)discsArray.get(in);

            if(cd.person == null)
            {
                cd.person = aPerson;
                discsArray.set(in, cd);
                return true;
            }
        }
        return false;
    } 

    public ArrayList<CD> getDiscsForPerson(Person aPerson) {
        ArrayList<CD> discsForPerson = new ArrayList<CD>();

        for (int index = 0; index < peopleArray.size(); index++)
            discsForPerson.add(discsArray.get(index));
        return discsForPerson;
    }

    public ArrayList<CD> getAvailableDiscs() {
        ArrayList<CD> AvailableDiscs = new ArrayList<CD>();
        for(int i = 0; i < discsArray.size(); i++)
                if(discsArray.get(i).getPerson() == null) {
                        AvailableDiscs.add(discsArray.get(i));
                }
        return AvailableDiscs;
    } 

    public ArrayList<CD> getUnavailableDiscs() {
        ArrayList<CD> UnavailableDiscs = new ArrayList<CD>();
        for(int i = 0; i < discsArray.size(); i++)
            if(discsArray.get(i).getPerson()  != null) {

                        UnavailableDiscs.add(discsArray.get(i));
                }
        return UnavailableDiscs;
    } 

    public String toString() {
        return "CD Library Name: " + name + "\n" + "Discs within the CD Store: " + discsArray + "\n" + "People within the CD store" + peopleArray;
    }

    public String getStatus(){
        String s="CDS taken: " + getUnavailableDiscs() + "CDS not taken: " + this.getAvailableDiscs();
        return s;
    }

    public void printStatus() {
        System.out.println("CDS taken: " + getUnavailableDiscs() + "\n"+ "CDS not taken: " + getAvailableDiscs() + "\n");
    } 

    public void saveCDStoreToSerialFile(String fileName)
    {
        try {
            ObjectOutputStream objOut = new ObjectOutputStream (
                new BufferedOutputStream (new FileOutputStream(fileName)));
                try 
                {
                    objOut.writeObject(this);
                    objOut.writeObject(name);
                    objOut.writeObject(discsArray);
                    objOut.writeObject(peopleArray);

                } 
                finally 
                {
                    objOut.close();
                }
            } 
        catch (Exception ex) 
        {
            ex.printStackTrace();
        }
    }


    public static CDStore loadCDStoreFromSerialFile(String fileName)
    {
    CDStore store = null;

    try {
        ObjectInputStream os = new ObjectInputStream(
                new BufferedInputStream(new FileInputStream(fileName)));
            try {
                Object obj = os.readObject();
                if (obj instanceof CDStore) {
                    store = (CDStore) obj;
                }
            } finally {
                os.close();
            }
        } 
        catch (Exception ex) {
            System.out.println("File not found");
        }
        return store;
    }; 
}

Recommended Answers

All 6 Replies

Please post the full text of the compiler's error message. It includes the source line where the error is.
Here is a sample:

TestSorts.java:138: cannot find symbol
symbol  : variable var
location: class TestSorts
     var = 2;
     ^

Hi NormR1, I think i've worked out 12 & 13 - I've changed:
discsArray = new ArrayList<CD>();
peopleArray = new ArrayList<Person>();
To
discsArray = new ArrayList[CD];
peopleArray = new ArrayList[Person];

And it doesn't appear on the list of errors anymore. The errors i'm left with are:

C:\temp>javac CD.java CDController.java CDStore.java Person.java
CDStore.java:7: <identifier> expected
        ArrayList<CD> discArray;
                 ^
CDStore.java:8: <identifier> expected
        ArrayList<Person> peopleArray;
                 ^
CDStore.java:20: <identifier> expected
        public ArrayList<CD> getDiscs() {
                        ^

3 errors

Where are the datatypes/classes CD and Person defined?

CD and Person are arrays which are created and populated in the CDController class file

The way they are used in two statements with errors makes the compiler think they are classes which the compiler can not find a definition for. What type of object do you want to put in those arraylists?
Change the CD and Person symbols used in the array definitions to be the names of a class, not the name of an array.

Its strange that you have ,java files with those names like they are classes:

CD.java CDController.java CDStore.java Person.java

I've changed:
discsArray = new ArrayList<CD>();
peopleArray = new ArrayList<Person>();
To
discsArray = new ArrayList[CD];
peopleArray = new ArrayList[Person];

Those changes completely destroy the original meaning of the code, so although they may compile it's unlikely that it will now work as intended.

What version of Java are you using for the command line? The <someClass> syntax wqas new with Java 1.5, so it always gives errors with earlier versions.

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.