Java Reference
In-Depth Information
String surname = in. readString ().trim();
return new Person(firstName,surname);
}
// Rest of the class as before...
}
You should have no trouble seeing how this works as it's almost identical to the readPerson()
method we used previously.
We can make the PhoneNumber class very simple:
class PhoneNumber implements Serializable {
public PhoneNumber(String areacode, String number) {
this.areacode = areacode;
this.number = number;
}
public String toString() {
return areacode + ' ' + number;
}
private String areacode;
private String number;
}
We could do a whole lot of validity checking of the number here, but it's not important for our example.
We could use a static method to read a number from the keyboard so let's add that too:
import java.io.*;
class PhoneNumber implements Serializable {
// Read a phone number from the keyboard
public static PhoneNumber readNumber() {
FormattedInput in = new FormattedInput();
int maxTries = 5; // Maximum number of errors
// in input
String area = null; // Stores the area code
String localcode = null; // Stores the local code
for(;;) { // Loop to allow retries
try {
// Read in the area code
if(area == null) { // If there's no area code
System.out.println("\nEnter the area code:");
area = Integer.toString(in.readInt()); // read one from the k/b
}
Search WWH ::




Custom Search