storing [a phone number] as ints would be inappropriate given their nature.
So why are you reading them in as ints? Why not use Scanner.next()
or Scanner.nextLine()
, since you know phone numbers aren't (conceptually) numbers?
You should expect your user to pass in exactly the phone number they expect. Attempting to transform it (e.g. letting them leave leading 0s off and filling them in yourself) is a recipe for confusion and bugs. Rather than transforming the user's input, add sanity checks, such as a regex, to confirm the user provided valid input.
public class nameEntry {
private static final Pattern VALID_NUMBER = Pattern.compile("\\d{4}");
private String surname;
private String number;
public nameEntry(String surname, String number) {
this.surname = surname;
if(!VALID_NUMBER.matcher(number).matches()) {
throw new IllegalArgumentException(number+
" does not appear to be a valid phone number.");
}
this.number = number;
}
@Override
public String toString() {
return surname + "\t" + number; //eg HOOD 0123
}
}