Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
import java.util.*;// Import all the classes in the util package
import javax.swing.*;
import java.io.*;
public class AddressBook
{
 ArrayList persons;
 public AddressBook()
 {
      persons=new ArrayList();
        loadPerson();
 }  
 public void loadPerson()
 {
 String tokens[]=null;
 String name,add,ph;

 try
 {
  FileReader fr=new FileReader("AddressBook.txt");
  BufferedReader br=new BufferedReader(fr);
  String s=br.readLine();
  while(s !=null )
  {
    tokens=s.split(",");
    name=tokens[0];
    add=tokens[1];
    ph=tokens[2];
    PersonInfo p=new PersonInfo(name,add,ph);
    persons.add(p);   
    s=br.readLine();
  }
     br.close();
     fr.close();
  }
  catch(IOException e)
  {
  System.out.println("unable to Load"); 
  }

 }
 public void addPerson()
 {
   String name=JOptionPane.showInputDialog("Enter Name");
   String address=JOptionPane.showInputDialog("Enter Address");
   String phoneNum=JOptionPane.showInputDialog("Enter PhoneNum");

   PersonInfo p=new PersonInfo(name,address,phoneNum);
   persons.add(p);
 }
 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();
  }
  }
 }       
      //remove
 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);
 }
 }
 }
 public void savePerson()
 {
 try
    {
     FileWriter fr=new FileWriter("mybook.txt");
     PrintWriter pw=new PrintWriter(fr);
     String s="";
     for(int i=0;i<persons.size();i++)
    {
     PersonInfo p=(PersonInfo)persons.get(i);
     s=p.name+","+p.address+","+p.phoneNum;
     pw.println(s);
    }
     pw.close();
     fr.close();    
    }
    catch(IOException e)
    {
     System.out.println("unable to write"); 
    }
 }

}


What I have tried:

i cant able to understand the problm im new in java
Posted
Updated 24-Aug-17 11:49am

The error points to this line:
Java
ph=tokens[2];
You are attempting to take the third element from the array, but the error is saying that there is no third element, so that index was "out of the bounds" of the array.

So, tokens only has two elements, which means that there is only one , in the string. If that was not intended, check your text file for correctness. If you believe it should be fine but it still crashes, use your debugger to run the code step-by-step and inspect the variables to see where it goes wrong. If you know that, you can fix the error.
 
Share this answer
 
Comments
Naveed Imran 25-Aug-17 4:55am    
thank u dear
With the help if debugger, you will be able to see what is in variables when error occurs, then you will understand why token is smaller than expected.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900