线程“main”中的异常地址簿中的addressbook.loadperson(addressbook.java:27)中的java.lang.arrayindexoutofboundsexception:2。< init>(addressbook.java:10) [英] Exception in thread "main" java.lang.arrayindexoutofboundsexception: 2 at addressbook.loadperson(addressbook.java:27) at addressbook.<init>(addressbook.java:10)

查看:54
本文介绍了线程“main”中的异常地址簿中的addressbook.loadperson(addressbook.java:27)中的java.lang.arrayindexoutofboundsexception:2。< init>(addressbook.java:10)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

}





我尝试过:



i无法理解java中的新问题



What I have tried:

i cant able to understand the problm im new in java

推荐答案

错误点到这一行:

The error points to this line:
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.


使用调试器的帮助,您将能够在发生错误时查看变量中的内容,然后您将了解为什么 token 小于预期。



有一个工具可以让你看到你的代码在做什么,它的名字是调试的。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]

调试器在这里向您展示你的代码正在做,你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。
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.


这篇关于线程“main”中的异常地址簿中的addressbook.loadperson(addressbook.java:27)中的java.lang.arrayindexoutofboundsexception:2。&lt; init&gt;(addressbook.java:10)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆