HI to all frndz! I have started learning Java and in a case study I have got some errors
I have written a class address book and an error is occurring due to following code snipet
persons.add(p);
and the error message is: "Address Book has tried to run an unchecked and unsafe method."
I am pasting the code of class Address Book and highlighting(by commenting) the code snippet due to which error is occurs. Please tell me remedy of this problem
import java.util.*;
import javax.swing.*;
public class AddressBook
{
private ArrayList persons;
// constructor of the class
public AddressBook()
{
persons = new ArrayList();
}
// method for adding person in address book
public void addPerson()
{
String name = JOptionPane.showInputDialog("Enter name of the person: ");
String address = JOptionPane.showInputDialog("Enter address of the person: ");
String phoneNum = JOptionPane.showInputDialog("Enter phoneNum of the person: ");
PersonInfo p = new PersonInfo(name, address, phoneNum);
persons.add(p); // above error due to this code
}
public void searchPerson(String n)
{
for(int i = 0; i<persons.size(); i++)
{
PersonInfo p = (PersonInfo)persons.get(i);
if(n.equals(p.name))
{
p.printPersonInfo();
}
}
}
public void deletePerson(String n)
{
for(int i = 0; i<persons.size(); i++)
{
PersonInfo p = (PersonInfo)persons.get(i);
if(n.equals(p.name))
{
persons.remove(i);
}
}
}
}
And i am facing same error for the class AddressBookTest also.
Following is the code
import javax.swing.*;
public class AddressBookTest
{
public static void main(String args[])
{
AddressBook add = new AddressBook();
while(true)
{
String input = JOptionPane.showInputDialog("Enter 1 to add" + "\n Enter 2 to search" + "\n Enter 3 to delete" + "\n Enter 4 to exit");
char in = input.charAt(0);
switch(in)
{
case 1:
add.addPerson();
break;
case 2:
String name = JOptionPane.showInputDialog("Enter name to search");
add.searchPerson(name);
break;
case 3:
String delete = JOptionPane.showInputDialog("Enter name to delete");
add.deletePerson(delete);
break;
case 4:
System.exit(0);
}
}
}
}