How to handle ArrayIndexedBoundexception in Java

122 Views Asked by At

I have been trying to get a specific columns from a csv file say having 30 columns but i need only 3 columns entirely when i execute the following code only i get only one entire column data..how to get 3 column data at a time.when i run it prints only one column...when i try to print multiple column it shows error message like

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at ReadCVS.main(ReadCVS.java:19)

public static void main(String[] args) throws Exception {
    String splitBy = ",";
    BufferedReader br = new BufferedReader(new FileReader("txt.csv"));
    String line = br.readLine();

    while((line = br.readLine()) !=null){
        String[] b = line.split(splitBy);

        PrintWriter out = new PrintWriter(new FileWriter("new.csv",true));

        out.println(b[0]);

        out.close();
    }
    br.close();
}
1

There are 1 best solutions below

11
On BEST ANSWER

The problem is probably is: You have only one line in your, txt.csv file.

When you called br.readLine(); for the first time, that line is read from the file and stored in String line variable. But you ignored that line, and you've read again, in your while condition:

while((line = br.readLine()) !=null)

So maybe you have an empty line or empty string after that first line. Then the while condition is true, but an empty String is stored in line variable. So the b[] has no element and b[0] is out of the bound.

One solution is to change this line:

String line = br.readLine();

to

String line = null;

[EDIT]

So if you try to read a file like the one in mkyong's site (as you linked in your comment) and split the lines by "," and write them in a new file for example, you can use a code like the code below:

public static void main(String[] args) throws IOException {
    BufferedWriter out = new BufferedWriter(new FileWriter("c:\\new.csv",true));
    BufferedReader br = new BufferedReader(new FileReader("c:\\txt.csv"));
    String splitBy = ",";
    String line = null;
    while((line = br.readLine()) !=null){
        StringBuffer newLine = new StringBuffer();
        String[] b = line.split(splitBy);
        for (int i = 0; i<b.length; i++)
        {
            if(b[i] == null || b[i].trim().isEmpty())
                continue;

            newLine.append(b[i].trim() + ";");
        }
        out.write(newLine.toString());
        out.newLine();
    }
    out.close();
    br.close();
}

Also you should know that the following line opens the output file in appendable way(the second boolean parameter in the constructor):

BufferedWriter out = new BufferedWriter(new FileWriter("c:\\new.csv",true));

Also I assumed the contents of the source file is the same as in mkyong's site, somethimg like this:

"1.0.0.0",,  ,  ,"1.0.0.255","16777216",  , "16777471","AU" ,, "Australia"
"1.0.1.0" ,  ,, "1.0.3.255" ,,   ,"16777472","16778239" , ,  "CN"  ,   ,"China"
"1.0.4.0","1.0.7.255","16778240","16779263","AU","Australia"
"1.0.8.0","1.0.15.255","16779264","16781311","CN","China"
"1.0.16.0","1.0.31.255","16781312","16785407","JP","Japan"
"1.0.32.0","1.0.63.255","16785408","16793599","CN","China"
"1.0.64.0","1.0.127.255","16793600","16809983","JP","Japan"
"1.0.128.0","1.0.255.255","16809984","16842751","TH","Thailand"

Good Luck.