| | |
cohort class compile errors
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2007
Posts: 6
Reputation:
Solved Threads: 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
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
•
•
Join Date: Apr 2006
Posts: 164
Reputation:
Solved Threads: 10
won't it be like following:
What is you object type in the input Student or aStudent?
Java Syntax (Toggle Plain Text)
private String update(Student aStudent)
A Perfect World
•
•
Join Date: Nov 2007
Posts: 6
Reputation:
Solved Threads: 0
•
•
•
•
won't it be like following:
What is you object type in the input Student or aStudent?Java Syntax (Toggle Plain Text)
private String update(Student aStudent)
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 [code ] tags around it. Unformatted code is a painful mess to read.
Also, check your spelling errors in the remove() and update() methods and if you re-post any code, make sure you place [code ] tags around it. Unformatted code is a painful mess to read.
![]() |
Other Threads in the Java Forum
- Previous Thread: java project database error
- Next Thread: converting JPEG colour image into gray scale image??
| Thread Tools | Search this Thread |
Tag cloud for Java
android api apple applet application arc arguments array arrays automation binary bluetooth c++ chat class classes client code codesnippet component csv database doctype draw ebook eclipse error event exception fractal freeze game givemetehcodez graphics gui html ide image input integer intellij iphone j2me java java.xls javaprojects jmf jni jpanel julia linux list loop loops mac map method methods mobile netbeans newbie number online oracle page parameter plazmic print problem program programming project recursion reporting rotatetext scanner screen sell server set size sms socket sort sourcelabs sql string superclass swing system template test testautomation threads time title tree tutorial-sample windows working






