• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Im having an errror

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
help please, im having an error that looks easy but for some reason i coudnt figure it out, can somebody help me please, probably a second eye might help..

heres my code:

[code]
import java.io.*;

public class StudentsFile
//manage file iname.dat
{


private static final BufferedReader stdin =

new BufferedReader(new InputStreamReader(System.in));

public static void main(String[] args) throws IOException

{

Student first = new Student(), second = new Student();



System.out.print("Enter the first student: ");

first.input(stdin);

System.out.print("Enter the second student: ");

second.input(stdin);

if (first.compareTo(second) < 0)

first.output();

else

second.output();


}

class Student implements Comparable, Cloneable
{
private String firstName, lastName;
private int credits;
private double gradePointAverage;
}
{
public int compareTo(Object other)
{
Student otherStudent = (Student)other;
String name1 = lastName + firstName;
String name2 = otherStudent.lastName + otherStudent.firstName;

return name1.compareTo(name2);

}

public boolean equals(Comparable other)
{
if (!(other instanceof Student))
return false;
Student o = (Student)other;
return name1.equals(o.name1) && name2.equals(o.name2);
}

public void input(Scanner reader) {
firstName = reader.next();
lastName = reader.next();
credits = reader.nextInt();
gradePointAverage = reader.nextDouble();
}

public void output()
{
System.out.println(lastName + ", " + firstName + " " + credits + " " + gradePointAverage);
}
}
}

[\code]

and the error im having are

illegal stary of expression
; expected

this error looks syntax error but i coudnt find it..
a little help will do plzzz..
thnks
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


You are closing out the class definition before you define the compareTo method.
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It usually helps if you post the exact error message you are receiving. Just copy/paste the message, it contains a lot of information pertaining to the cause and location of the error.

The error you're recieving is probably here:

After that gets cleared up you'll likely recieve a host of other error messages. Like your input() method specifies a Scanner parameter but you are passing a BufferedReader. Also if Student class is an inner-class of StudentsFile it must be declared static in order to access it from the static main() method. There are a few other errors in there also.
 
jayson clark
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for your replies, as i compile my program you are right im having tons of errors, so what i did was i code it one class at a time..so i did first the student class method, i wanted this to work before i tested it to with the driver...but im having another error..

error: Illegal Start of Expression in line 51 --> public void input(BufferedReader reader) {

and ; expected line line 70 which thethe end of the program..
herte my ocde for the student class



please help... thanks
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


You forgot to add a second closing brace at the end of the first input method. Also you cant have two input() methods with exactly the same signiture. How would the JVM figure out which one to call?
 
jayson clark
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Garrett, i revised my codes coz i figure out its all messed up, thanks for your help..now in my new code i think its more clear but im having a new set of errors and i need your help again, sorry im trying and im really new in java..

[code]
import java.io.*;
class Students implements Comparable
{
private String firstName, lastName, name1, name2;
private int credits;
private double gradePointAverage;
private boolean nullStudent = false;
// The exception thrown for names without a space
// separating the first name and last name
public class NameFormatError extends IOException {}

public int compareTo(Object other)
{
Student otherStudent = (Student)other;
String name1 = lastName + firstName;
String name2 = otherStudent.lastName + otherStudent.firstName;
return name1.compareTo(name2);
}

public boolean equals(Comparable other)
{
if (!(other instanceof Student))
return false;
Student o = (Student)other;
return name1.equals(o.name1) && name2.equals(o.name2);
}
public boolean isNull()
{
return nullStudent;
}
public void input(BufferedReader reader) throws IOException
{
String line = reader.readLine();
if (line == null)
{
nullStudent = true;
return;
}
//indexOf() and substring();
{

if (line == null)
return;
int space = line.indexOf(' ');
if (space < 0)
throw new NameFormatError();

firstName = (line.substring(0, space));
int space2 = line.indexOf(' ', space + 1);
lastName = (line.substring(space + 1, space2));
space = line.indexOf(' ', space2 + 1);
credits = Integer.parseInt(line.substring(space2 + 1, space));
gradePointAverage = Double.parseDouble(line.substring(space + 1));

{
System.out.println(firstName + ", " + lastName + " " + credits + " " +
gradePointAverage);
}
}
}
}
[\code]

errors im getting:

lastName has private access in Students
firstname has private access in Students
name1 has private access in Students
name2 has private access in Students..

plz bare with me i really need help..thanks much..
[ February 19, 2006: Message edited by: jayson clark ]
 
What a show! What atmosphere! What fun! What a tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic