ArrayOutofBoundsException - 尝试读取/从文件读入哈希映射

时间:2015-03-09 23:13:21

标签: java

我正在从事家庭作业并遇到一个奇怪的" ArrayOutOfBoundsException"错误 - 我知道错误意味着什么(基本上我试图引用数组中不存在的位置),但我不确定它为什么会抛出该错误?我不确定自己错过了什么,但显然在我某处看不到某些逻辑错误。

PhoneDirectory.java

import java.util.HashMap;
import java.io.*;

class PhoneDirectory {
    private HashMap<String, String> directoryMap;
    File directory;
    public PhoneDirectory() { //create file for phone-directory
        directory = new File("phone-directory.txt");
        directoryMap = new HashMap<String, String>();
        try(BufferedReader buffer = new BufferedReader(new FileReader(directory))) {
            String currentLine;
            while((currentLine = buffer.readLine()) != null) { //set currentLine = buffer.readLine() and check if not null
                String[] fileData = currentLine.split(","); //create array of values in text file - split by comma
                directoryMap.put(fileData[0], fileData[1]); //add item to directoryMap
            }
        }
        catch(IOException err) {
            err.printStackTrace();
        }
    }
    public PhoneDirectory(String phoneDirectoryFile) {
        directory = new File(phoneDirectoryFile);
        directoryMap = new HashMap<String, String>();
        try(BufferedReader buffer = new BufferedReader(new FileReader(directory))) {
            String currentLine;
            while((currentLine = buffer.readLine()) != null) { //set currentLine = buffer.readLine() and check if not null
                String[] fileData = currentLine.split(","); //create array of values in text file - split by comma
                directoryMap.put(fileData[0], fileData[1]); //add item to directoryMap
            }
        }
        catch(IOException err) {
            err.printStackTrace();
        }
    }
    public String Lookup(String personName) {
        if(directoryMap.containsKey(personName))
            return directoryMap.get(personName);
        else
            return "This person is not in the directory.";
    }
    public void AddOrChangeEntry(String name, String phoneNumber) {
        //ASK IF "IF-ELSE" CHECK IS NECESSARY
        if(directoryMap.containsKey(name))
            directoryMap.put(name,phoneNumber); //if name is a key, update listing
        else
            directoryMap.put(name, phoneNumber); //otherwise - create new entry with name
    }
    public void DeleteEntry(String name) {
        if(directoryMap.containsKey(name))
            directoryMap.remove(name);
        else
            System.out.println("The person you are looking for is not in this directory.");
    }
    public void Write() {
        try(BufferedWriter writeDestination = new BufferedWriter(new FileWriter(directory)))
        {
            for(String key : directoryMap.keySet())
            {
                writeDestination.write(key + ", " + directoryMap.get(key) + '\n');
                writeDestination.newLine();
            }
        }
        catch(IOException err) {
            err.printStackTrace();
        }
    }
}

Driver.java

public class Driver {
    PhoneDirectory list1;
    public static void main(String[] args) {
        PhoneDirectory list1 = new PhoneDirectory("test.txt");
        list1.AddOrChangeEntry("Disney World","123-456-7890");
        list1.Write();
    }

}

基本上我正在创建一个名为&#34; test.txt&#34;的文件。并添加了“迪斯尼世界”,123-456-7890&#34; - 奇怪的是,代码仍然有效 - 但无论如何它都会引发我的错误,那么真正发生了什么? (为了记录,我指的是这些行:directoryMap.put(fileData [0],fileData [1]) - 分别是第14行和第28行。)

0 个答案:

没有答案