Reputation: 53
I created a student class and I need to use the students first name followed by their code (first student is 1000, nxt is 1001) to create a loginId Using first letter of name + last name (or if last name is longer than 4 letters just 4 letters of the last name) + ending digits of their code for example John Baker would be, jbake00
public class Student
{
//Instance variables
private double coursecount = 0;
private static int lastAssignedNumber = 1000;
private double credit;
private String course;
//Variables
public String name;
public String address;
public String loginId = "";
public int accountNumber;
public double gpa;
//Constructs new student
public Student(String name) {
this.name = name;
this.accountNumber = lastAssignedNumber;
lastAssignedNumber++;
setloginid();//edited this one in
}
public void setloginId() {
int position = this.name.indexOf(' ');
String first_name = this.name.substring(0,1);
String last_name = this.name.substring(position + 1);
if(last_name.length() >= 4)
last_name = last_name.substring(0,4);
first_name = first_name.toLowerCase();
last_name = last_name.toLowerCase();
String digit_word = new Integer(accountNumber).toString();
String digit_short = digit_word.substring(2);
loginId += first_name + last_name + digit_short;
this.loginId = loginId;
}
The problem i have here is that loginId isn't being saved into the global variable why is that.
Upvotes: 0
Views: 80
Reputation: 10086
You need to call the setloginId()
method somewhere. From your comments, you seem to want to do it in the constructor:
im just creating that constructor to try to set the loginId as a value
As follows:
public Student(String name) {
this.name = name;
this.accountNumber = lastAssignedNumber;
lastAssignedNumber++;
setloginId(); //need to call this
}
You may also want to privatize your setloginId()
method as it's not necessary to expose it:
private void setloginId() {
Also a minor change, you can change:
loginId += first_name + last_name + digit_short;
this.loginId = loginId;
to:
this.loginId = first_name + last_name + digit_short;
It's not necessary to do +=
as it is going to append to the existing string, which you probably don't want.
Upvotes: 2
Reputation: 5233
You have to run the method setLoginID from the constructor of your object. And you can make the method a private method, i don't think there is any use in accessing it from anywhere else than from your constructor
Upvotes: 0