How can I get rid of this error without adding \"throws ValidationException\" in
ID: 645750 • Letter: H
Question
How can I get rid of this error without adding "throws ValidationException" into the method addPerson()?
heres the classes needed...
Class AddressBook
import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
import java.util.Scanner;
import javax.xml.bind.ValidationException;
public class AddressBook {
List<Person> persons;
Scanner input;
private static final int ADD_PERSON = 1;
private static final int REMOVE_PERSON = 2;
private static final int SORT_FULL_NAME = 3;
private static final int VIEW_PERSONS = 4;
private static final int EXIT = 5;
public AddressBook() {
persons = new ArrayList();
input = new Scanner(System.in);
}
public void showMenu(){
System.out.println("1 Add Person");
System.out.println("2 Remove Person");
System.out.println("3 Sort Full Name");
System.out.println("4 View Persons");
System.out.println("5 Exit");
}
public void runAddressBook(){
showMenu();
int option;
option = input.nextInt();
do {
switch (option) {
case ADD_PERSON:
addPerson();
break;
case REMOVE_PERSON:
removePerson();
break;
case SORT_FULL_NAME:
sortByFullName();
break;
case VIEW_PERSONS:
viewPersons();
break;
case EXIT:
break;
}
} while (option != EXIT);
}
public void addPerson() {
System.out.println("Please enter full name");
String name = input.next();
System.out.println("Please enter phone number");
String number = input.next();
System.out.println("Please enter email address");
String email = input.next();
//new Person(fullName, phoneNumber, email);
persons.add(new Person(name, number, email));
}
public void removePerson() {
System.out.println("Enter index of person you wish to remove");
int index = input.nextInt();
persons.remove(index);
}
public void sortByFullName() {
Collections.sort(persons);
}
public void viewPersons() {
for(int i = 0; i < persons.size(); i++) {
System.out.println("Person " + i);
System.out.println("Name " + persons.get(i).getFullName());
System.out.println("Phone Number: " + persons.get(i).getPhoneNumber());
System.out.println("Email " + persons.get(i).getEmail());
System.out.println();
}
}
}
Class Person
import javax.xml.bind.ValidationException;
public class Person {
private String fullName, phoneNumber, email;
public Person(String fullName, String phoneNumber, String email) throws ValidationException {
validateString(fullName, "Full Name", 50);
this.fullName = fullName;
validateString(phoneNumber, "Phone Number", 15);
this.phoneNumber = phoneNumber;
validateString(email, "Email", 35);
this.email = email;
}
public Person() throws ValidationException{
this("unknown", "unknown", "unknown");
}
public String getFullName(){
return fullName;
}
public void setFullName(String fullName) throws ValidationException
{
validateString(fullName, "Full Name", 50);
this.fullName = fullName;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) throws ValidationException {
validateString(phoneNumber, "Phone Number", 15);
this.phoneNumber = phoneNumber;
}
public String getEmail(){
return email;
}
public void setEmail(String email) throws ValidationException {
validateString(email, "Email", 35);
this.email = email;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(fullName);
sb.append(",");
sb.append(phoneNumber);
sb.append(",");
sb.append(email);
return sb.toString();
}
private void validateString(String value, String fieldName, int maxLength) throws ValidationException
{
if(value.length() > maxLength)
{
throw new ValidationException(fieldName + " exceeds " + maxLength + " characters.");
}
if(value == null || value.equals("")){
throw new ValidationException(fieldName +" cannot be null or only be a white space");
}
if(value.contains(",")){
throw new ValidationException(fieldName +" cannot contain a comma");
}
}
}
Class ValidationException
public class ValidationException extends Exception{
public ValidationException(){
super.getMessage();
}
public ValidationException(String message){
super(message);
}
public ValidationException(String message, Throwable throwable){
super(message, throwable);
}
public ValidationException(Throwable throwable){
super(throwable);
}
}
Explanation / Answer
Program code:
//ValidationException.java
public class ValidationException extends Exception
{
public ValidationException()
{
super.getMessage();
}
public ValidationException(String message)
{
super(message);
}
public ValidationException(String message, Throwable throwable)
{
super(message, throwable);
}
public ValidationException(Throwable throwable)
{
super(throwable);
}
}
//Person.java
import java.util.Comparator;
import javax.xml.bind.ValidationException;
public class Person
{
private String fullName, phoneNumber, email;
public Person() throws ValidationException
{
this("unknown", "unknown", "unknown");
}
public Person(String fullName, String phoneNumber, String email) throws ValidationException
{
validateString(fullName, "Full Name", 50);
this.fullName = fullName;
validateString(phoneNumber, "Phone Number", 15);
this.phoneNumber = phoneNumber;
validateString(email, "Email", 35);
this.email = email;
}
public String getFullName()
{
return fullName;
}
public void setFullName(String fullName) throws ValidationException
{
validateString(fullName, "Full Name", 50);
this.fullName = fullName;
}
public String getPhoneNumber()
{
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) throws ValidationException
{
validateString(phoneNumber, "Phone Number", 15);
this.phoneNumber = phoneNumber;
}
public String getEmail()
{
return email;
}
public void setEmail(String email) throws ValidationException
{
validateString(email, "Email", 35);
this.email = email;
}
@Override
public String toString()
{
StringBuilder sb = new StringBuilder();
sb.append(fullName);
sb.append(",");
sb.append(phoneNumber);
sb.append(",");
sb.append(email);
return sb.toString();
}
private void validateString(String value, String fieldName, int maxLength)
throws ValidationException
{
if (value.length() > maxLength)
{
throw new ValidationException(fieldName + " exceeds " + maxLength + " characters.");
}
if (value == null || value.equals(""))
{
throw new ValidationException(fieldName + " cannot be null or only be a white space");
}
if (value.contains(","))
{
throw new ValidationException(fieldName + " cannot contain a comma");
}
}
}
//AddressBook.java
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.ArrayList;
import java.util.Scanner;
import javax.xml.bind.ValidationException;
public class AddressBook
{
List<Person> persons;
Scanner input;
private static final int ADD_PERSON = 1;
private static final int REMOVE_PERSON = 2;
private static final int SORT_FULL_NAME = 3;
private static final int VIEW_PERSONS = 4;
private static final int EXIT = 5;
public AddressBook()
{
persons = new ArrayList<Person>();
input = new Scanner(System.in);
}
public void showMenu()
{
System.out.println("1 Add Person");
System.out.println("2 Remove Person");
System.out.println("3 Sort Full Name");
System.out.println("4 View Persons");
System.out.println("5 Exit");
}
public void runAddressBook()
{
showMenu();
int option;
option = input.nextInt();
do
{
switch (option)
{
case ADD_PERSON:
addPerson();
break;
case REMOVE_PERSON:
removePerson();
break;
case SORT_FULL_NAME:
sortByFullName();
break;
case VIEW_PERSONS:
viewPersons();
break;
case EXIT:
break;
}
} while (option != EXIT);
}
public void addPerson()
{
System.out.println("Please enter full name");
String name = input.next();
System.out.println("Please enter phone number");
String number = input.next();
System.out.println("Please enter email address");
String email = input.next();
// new Person(fullName, phoneNumber, email);
try
{
persons.add(new Person(name, number, email));
} catch (Exception e)
{
e.printStackTrace();
}
}
public void removePerson()
{
System.out.println("Enter index of person you wish to remove");
int index = input.nextInt();
persons.remove(index);
}
public void sortByFullName()
{
Collections.sort(persons, byFullName());
}
public void viewPersons()
{
for (int i = 0; i < persons.size(); i++)
{
System.out.println("Person " + i);
System.out.println("Name " + persons.get(i).getFullName());
System.out.println("Phone Number: " + persons.get(i).getPhoneNumber());
System.out.println("Email " + persons.get(i).getEmail());
System.out.println();
}
}
private static Comparator<Person> byFullName()
{
return new Comparator<Person>()
{
@Override
public int compare(Person x, Person y)
{
return x.getFullName().compareTo(y.getFullName());
}
};
}
}
Note:
Whenever there is problem is handling the Exceptions, then try to put the code in the try..catch block.
The changes made are highlighted in bold letters. The changes made are:
When you are storing List of objects of the person class, then while initializing the list also should contain the type of ArrayList you are going to store.
persons = new ArrayList<Person>();
The when the sort method is called upon the List persons it should contain an override method of compare which should contain the logic on what bases you want to sort the List of objects.
Here, in this code, the sorting is done by the Full name.
Hope these changes will help the code to execute.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.