为什么我收到错误“java.lang.ArrayIndexOutOfBoundsException:1”

时间:2017-09-06 21:41:43

标签: java arrays csv

当我运行它时,我收到以下错误:

  

线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:1   在FileConverter.main(FileConverter.java:36)

     

使用退出代码1完成处理

当我运行它时,它会在我输入的路径中保存一个文件,但它是一个没有数据的空文件,任何人都可以帮助我。谢谢。

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class FileConverter {

    static Map<String, String> states = new HashMap<String, String>();
    static Scanner keyboard;


    public static void main(String[] args) {
        loadStates();
        System.out.println("Enter the path and name of input file with "
                + "format");
        keyboard = new Scanner(System.in);
        String fileIn = keyboard.nextLine();
        System.out.println("Enter the path and file name for the output file "
                + " with format");

        String fileOut = keyboard.nextLine();
        try {
            BufferedReader br = new BufferedReader(new FileReader(fileIn));
            BufferedWriter out = new BufferedWriter(new FileWriter(fileOut));
            String sCurrentLine;
            br.readLine();
            while ((sCurrentLine = br.readLine()) != null) {
                String[] token = sCurrentLine.split("\t");
                String firstname = token[0].replaceAll("\"", "");
                String secondName = token[1];
                String[] streetPlusZipcode = token[2].replaceAll("\"", "").split(",");
                String street_address = streetPlusZipcode[0];
                String[] token2 = streetPlusZipcode[1].trim().split(" ");
                String street_abbr = token2[0];
                street_abbr = states.get(street_abbr.toLowerCase());
                String zipcode = token2[1];

                out.write(firstname+",");
                out.write(secondName+",");
                out.write(street_address+",");
                out.write(street_abbr+",");
                out.write(zipcode);
                out.newLine();
            }
            System.out.println("Successfully converted");
            out.close();
        } catch (FileNotFoundException ex) {
            System.out.println(ex);


        } catch (IOException ex) {
            System.out.println(ex);
        }

    }

    public static void loadStates() {
        states.put("alabama", "AL");
        states.put("alaska", "AK");
        states.put("alberta", "AB");
        states.put("american Samoa", "AS");
        states.put("arizona", "AZ");
        states.put("arkansas", "AR");
        states.put("armed forces (AE)", "AE");
        states.put("armed forces americas", "AA");
        states.put("armed forces pacific", "AP");
        states.put("british columbia", "BC");
        states.put("california", "CA");
        states.put("colorado", "CO");
        states.put("connecticut", "CT");
        states.put("delaware", "DE");
        states.put("district of columbia", "DC");
        states.put("florida", "FL");
        states.put("georgia", "GA");
        states.put("guam", "GU");
        states.put("hawaii", "HI");
        states.put("idaho", "ID");
        states.put("illinois", "IL");
        states.put("indiana", "IN");
        states.put("iowa", "IA");
        states.put("kansas", "KS");
        states.put("kentucky", "KY");
        states.put("louisiana", "LA");
        states.put("maine", "ME");
        states.put("manitoba", "MB");
        states.put("maryland", "MD");
        states.put("massachusetts", "MA");
        states.put("michigan", "MI");
        states.put("minnesota", "MN");
        states.put("mississippi", "MS");
        states.put("missouri", "MO");
        states.put("montana", "MT");
        states.put("nebraska", "NE");
        states.put("nevada", "NV");
        states.put("new brunswick", "NB");
        states.put("new hampshire", "NH");
        states.put("new jersey", "NJ");
        states.put("new mexico", "NM");
        states.put("new york", "NY");
        states.put("newfoundland", "NF");
        states.put("north carolina", "NC");
        states.put("north dakota", "ND");
        states.put("northwest territories", "NT");
        states.put("nova scotia", "NS");
        states.put("nunavut", "NU");
        states.put("ohio", "OH");
        states.put("oklahoma", "OK");
        states.put("ontario", "ON");
        states.put("oregon", "OR");
        states.put("pennsylvania", "PA");
        states.put("prince edward island", "PE");
        states.put("puerto rico", "PR");
        states.put("quebec", "QC");
        states.put("rhode island", "RI");
        states.put("saskatchewan", "SK");
        states.put("south carolina", "SC");
        states.put("south dakota", "SD");
        states.put("tennessee", "TN");
        states.put("texas", "TX");
        states.put("utah", "UT");
        states.put("vermont", "VT");
        states.put("virgin islands", "VI");
        states.put("virginia", "VA");
        states.put("washington", "WA");
        states.put("west virginia", "WV");
        states.put("wisconsin", "WI");
        states.put("wyoming", "WY");
        states.put("yukon territory", "YT");
    }
}

0 个答案:

没有答案