0

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

Edited by Dani: Formatting fixed

4
Contributors
8
Replies
9
Views
8 Years
Discussion Span
Last Post by Ezzaral
0

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

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

0

Which one is your line 46?

sorry it is:

private String update(aStudent aStudent)

0

won't it be like following:

private String update(Student aStudent)

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

0

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

0

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.

0

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

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.