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

 }  

}

Before Java 5 arraylists just contained any kind of objects. Java 5 allowed you to specify exactly what kind of objects a list can contain, which prevents a lot of errors. Now Java flags the old way as unsafe.
The way to do it in modern Java is
ArrayList<PersonInfo> persons = new ArrayList<>();

That tells Java that the list can only accept PersonInfo objects, and that anything you get from the list will be a PersinInfo.

Edited 1 Year Ago by JamesCherrill

This article has been dead for over six months. Start a new discussion instead.