Firstname = new FirstName;
Lastname = new LastName;
studentID = new StudentID;
The ‘new’ is not required on these lines, you are assigning an already existent String to a variable, not creating a new object. Also, Java is case-sensitive and you have nothing in your code named FirstName, only Firstname.
From a stylistic viewpoint, and style is important to avoid confusion when communicating with other programmers, variable names should start with a lower-case letter. “firstName” is preferable to “Firstname” or “FirstName”.
if (Firstname = " ") {
Firstname = "Blank first name";
}
if (Lastname = " ") {
Lastname = "Blank last name";
}
The operator for whether two objects are the same in Java is ‘==’ and not ‘=’, which is used to assign a value. In fact ‘==’ is almost certainly not what you wanted anyway. Java is a bit complicated on this issue for beginners but here goes: The statement “if (a == b)…” means “is the object in ‘a’ the same object as in ‘b’” which is different, in general, from “does the object in ‘a’ have the same value as that in ‘b’”. This is because two separate objects can have the same value. In Java you would use “a.equals(b)” to test if they have equal value.
In the case of checking for blank or empty Strings however, a better solution would be :
if (Firstname.trim().isEmpty()) {
Firstname = "Blank first name";
}
if (Lastname.trim().isEmpty() = " ") {
Lastname = "Blank last name";
}
“Firstname.trim()” returns a copy of Firstname with all blanks stripped out, so if it contained only blanks it would return an empty String. “.isEmpty()” then tests to see if the result is empty, which will be true if “Firstname” was either empty itself or contained only blanks. I think this is what you wanted.
It’s been a long time since I did Java, but I think you have some variable name conflicts in your constructor as well. You have member variables Firstname, Lastname, and stundentID which you also pass as arguments to your constructor. Also, your string allocations in the constructor are wrong (I’m not even sure if you need to allocate them with new… you might be able to just assign them). You definitely don’t need to “new” your int member variable. Your comparison operator is also wrong. Your constructor should be something like this:
public void student(String firstname, String lastname, int studentid)
{
Firstname = new String(firstname);
Lastname = new String(lastname);
studentID = studentid;
if (Firstname == " ") {
Firstname = "Blank first name";
}
if (Lastname == " ") {
Lastname = "Blank last name";
}
}
You would then create your object with:
student myStudent = new student(“John”, “Smith”, 1)
This naming issue is a good reason to use a naming convention like appending underscores to member variables (i.e. your members would be firstName_, lastName_, studentID_).
And yes, the error that FordPrefect identified also needs to be fixed.
And yeah, on preview, check for empty strings like ticker says.
No snark intended – did your compiler not give you a list of errors?
If you’re in a situation where you are not seeing what the compile errors are then your first step should be to rectify that; it’s almost impossible to implement anything non-trivial with the only feedback being “worked” or “didn’t work”.
If you are seeing errors, then they should be the first thing to check. I can’t remember what additional help there is for, say, javac but if you don’t understand the error’s terminology, you can google an error message for just about any compiler, and the first hit you’ll get is usually a full description of the error, with example(s).
(Obviously sometimes compile errors can be misleading, but in this case I think the errors would be pretty accurate).
Otherwise, especially with rampant strange spelling of names these days, the breakpoint between last and first name could be hard to decipher in some cases.