Hi i have made a student and cohrt collection class program
The compiler give me error: illegal start of expression line 46

The cohort class file:

import java.util.*;

public class Cohort {

    private static int groupSize = 15;

    private HashSet aCohort;

    public Cohort()
    {
        aCohort = new HashSet();
    }   



    public String add(Student aStudent)
    {   
        if (aCohort.add(aStudent))

            if (aCohort.add(aStudent))
            {

                return "success";
            }

        return "failure";
    }



    public String remove(String aSudent)
    {
        Iterator it = aCohort.iterator();   
        while (it.hasNext())
        {
            Student aStudent = (Student)it.next();
            if (aStudent== aStudent.getName()) { 
            {   
                it.remove();    
                return "success"; 
            }
        }
        return "failure";
    }

    private String update(aStudent aStudent)
    {
        if (remove(Student.getName()) == "success")
        {
            return add(aStudent);
        }
        return "failure";
    }

    public Student search(String aName)
    {   
        Iterator it = aCohort.iterator();
        while (it.hasNext())
        {
            Student aStudent = (Student)it.next();
            if (aName == aStudent.getName())
            {
                return aStudent;
            }
        }
        return null;
    }




    public String toString()
    {
        String s = new String();
        Iterator it = aCohort.iterator();
        while (it.hasNext())
        {
            Student aStudent = (Student)it.next();
            s = s + aStudent.toString();
            if (it.hasNext())
            {
                s = s + "\n";
            }
        }
        return s;
    }


    public static void main(String args[])
    {

        Cohort aCohort = new Cohort();

        Student aStudent = new Student("Nathan","Leicester Uk");
        System.out.println("Add student to Cohort: " + aCohort.add(aStudent));
        System.out.println(aCohort.toString());


        Student bStudent = new Student("Steve","Manchester Uk");
        System.out.println("Add student to Cohort: " + aCohort.add(bStudent));
        System.out.println(aCohort.toString());

Any help would be greatly appreciated

Recommended Answers

All 8 Replies

Which one is your line 46?

your remove() method doesn't have a closing brace

perhaps this has to do with your two opening braces after the if

Which one is your line 46?

sorry it is:

private String update(aStudent aStudent)

won't it be like following:

private String update(Student aStudent)

What is you object type in the input Student or aStudent?

won't it be like following:

private String update(Student aStudent)

What is you object type in the input Student or aStudent?

this is the student class program:

public class Student {
private static int nextNumber=1;
private int number;
private String name;
private String address;

public Student(String aName,String anAddress)
{
number = nextNumber;
name = aName;
address = anAddress;
nextNumber++;
}

public void setName(String aName)
{
name = aName;
}

public void setAddress(String anAddress)
{
address = anAddress;
}

public static int getNextNumber()
{
return nextNumber;
}

public int getNumber()
{
return number;
}

public String getName()
{
return name;
}


public String getAddress()
{
return address;
}

public String toString()
{
return "Number: " + number + " Name: " + name + " Address: " + address;
}


public static void main(String args[]){
Student aStudent = new Student("Nathan","Leicester England");
System.out.println("Number: " +aStudent.getNumber());
System.out.println("Name: " +aStudent.getName());
System.out.println("Address: " +aStudent.getAddress());

Student bStudent = new Student("Martin","St Margerets College Leicester");

System.out.println("Number: " +bStudent.getNumber());
System.out.println("Name: " +bStudent.getName());
System.out.println("Address: " +bStudent.getAddress());

bStudent.setName("Steven");
bStudent.setAddress("Abby Park College Leicester");
System.out.println(bStudent.toString());
}
}


I am all very new to Java and are having trouble understanding the error messages.

Thanks again

Most likely you are missing a brace or semi-colon somewhere above that line. Check all of your code blocks for proper brace structure and make sure all of your statements have appropriate semi-colons.

Also, check your spelling errors in the remove() and update() methods and if you re-post any code, make sure you place tags around it. Unformatted code is a painful mess to read.[code ] tags around it. Unformatted code is a painful mess to read.

Thanks for your help. I did find a loose curly bracket nd one spelling mistake. I now have 3 errors in the compiler all pointing to line 34:

[ while (it.hasNext()) ]

errors state: illegal start of type, <identifier> expected and ';' expected.

Thanks again for everyones help and advice

You still have an error with the block structure then, because there is nothing wrong with that line.

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.