سؤال

I'm creating a phone directory which will consist of multiple nameEntry objects each containing a surname and a phone extension.

public class nameEntry {

    public String surname;
    public String number;

    public nameEntry(String surname, int number) {
        this.surname = surname.toUpperCase();
        this.number = String.format("%04d", number); 
        // converts int literal to 0 filled string of numbers
    }

    public String toString() {
        return surname + "\t" + number; //eg HOOD     0123
    }
} 

Having read into good java practice about storing alphanumerics I have programmed the nameEntry objects to take an int as a parameter and then convert it to a zero filled String, because storing as int Objects would be inappropriate given their nature. However, this approach means that I cannot create new nameEntry objects with zero filled numbers by this method as it tells me the integers are invalid;

nameEntry e = new nameEntry(surname, 0123); //this would be flagged as wrong

However if I use a Scanner nextInt(System.in) method, is it perfectly happy with me entering a zero filled digit and then using that object to create a new nameEntry object.

Why is this scenario valid?

Regardless, this all seems over the top just to get a single zero filled input string - what is a better way of allowing a user to enter a single number of up to 4 characters that is either zero filled, or will be automatically zero filled after entry?

هل كانت مفيدة؟

المحلول

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
  }
}

نصائح أخرى

When parsing an integer value from a String (including when you use a Scanner and its nextInt() method), Java simply ignores the leading zero when the radix is 10 (decimal, which is the default).

String bar = "0123";
int foo = Integer.valueOf(bar);
System.out.println(foo);

Output:

123

If you need to specifically exclude input that has a leading 0 then you would need to read the input as a String and check for yourself whether or not there's a leading zero.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top