Hi, please assist;

I'm trying to read a text file located on a url, and that file information is separatedby tabs, and some cases the results on it are null values, depending on the systems performance.

now, I can't read it if all values are tabs, especially the last values, but when I have the text file saved on my hard drive I can read it.

I get the following error message:
http://10.113.239.150/data/mmchInt.20100823.txt
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
Site Hits Kbytes Visits Users Anon hits Errors WAP1 hits WAP2 hits Ave time (ms)
at dashboardclasses.readINTEGRA.readURLINTEGRA(readINTEGRA.java:68)
at dashboardclasses.Main.main(Main.java:24)
Java Result: 1

Here is the sample of text file:::


Site Hits Kbytes Visits Users Anon hits Errors WAP1 hits WAP2 hits Ave time (ms)
services 1 0
Service 2 0
Service 3 4021880 11668322 114909 62390 1037 115757 4021880 0
Service 4 0
Service 5 0


The class that reads the file:::

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 *
 * @author Lehlohonolo
 */
public class readINTEGRA extends dbConnection {

    public void readURLINTEGRA() throws SQLException {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        calendar.roll(Calendar.DAY_OF_MONTH, -1);
        boolean fline = true;
        URL urlWG = null;

        //INTEGRA URL: [url]http://10.113.239.150/data/mmchInt.20100606.txt[/url]
        try {
            urlWG = new URL("http://10.113.239.150/data/mmchInt." + dateFormat.format(calendar.getTime()) + ".txt");
            BufferedReader in = new BufferedReader(new InputStreamReader(urlWG.openStream()));
            System.out.println(urlWG);
            String str;
            int INTanonUsers = 0, INTusers = 0, INTerrors = 0, INTwap1hits = 0;
            int INThits = 0, INTkbytes = 0, INTvisits = 0, INTwap2hits = 0, INTaveTime = 0;

            while ((str = in.readLine()) != null) {
                if (fline != false) {
                    System.out.println(str);
                    fline = false;
                } else {
                    String tab[] = str.split("\t", -1);

                    String INTsite = tab[0];
                    try {
                        String hits = tab[1];
                        if (hits.length() != 0) {
                            INThits = Integer.parseInt(hits);
                        } else {
                            INThits = 0;
                        }
                    } catch (NullPointerException e) {
                        System.out.println("mag hits faulty");
                    }
                    try {
                        String bytes = tab[2];
                        if (bytes.length() != 0) {
                            INTkbytes = Integer.parseInt(bytes);
                        } else {
                            INTkbytes = 0;
                        }
                    } catch (NullPointerException e) {
                        System.out.println("kbytes hits faulty");
                    }
                    try {
                        String visits = tab[3];
                        if (visits.length() != 0) {
                            INTvisits = Integer.parseInt(visits);
                        } else {
                            INTvisits = 0;
                        }
                    } catch (NullPointerException e) {
                        System.out.println("visits hits faulty");
                    }
                    try {
                        String users = tab[4];
                        if (users.length() != 0) {
                            INTusers = Integer.parseInt(users);
                        } else {
                            INTusers = 0;
                        }
                    } catch (NullPointerException e) {
                        System.out.println("users hits faulty");
                    }
                    try {
                        String anonU = tab[5];
                        if (anonU.length() != 0) {
                            INTanonUsers = Integer.parseInt(anonU);
                        } else {
                            INTanonUsers = 0;
                        }
                    } catch (NullPointerException e) {
                        System.out.println("anonusers hits faulty");
                    }
                    try {
                        String errors = tab[6];
                        if (errors.length() != 0) {
                            INTerrors = Integer.parseInt(errors);
                        } else {
                            INTerrors = 0;
                        }
                    } catch (NullPointerException e) {
                        System.out.println("errors hits faulty");
                    }
                    try {
                        String w1hits = tab[7];
                        if (w1hits.length() != 0) {
                            INTwap1hits = Integer.parseInt(w1hits);
                        } else {
                            INTwap1hits = 0;
                        }
                    } catch (NullPointerException e) {
                        System.out.println("wap1hits hits faulty");
                    }
                    try {
                        String w2hits = tab[8];
                        if (w2hits.length() != 0) {
                            INTwap2hits = Integer.parseInt(w2hits);
                        } else {
                            INTwap2hits = 0;
                        }
                    } catch (NullPointerException e) {
                        System.out.println("wap2hits hits faulty");
                    }
                    try {
                        String avetime = tab[9];
                        if (avetime.length() != 0) {
                            INTaveTime = Integer.parseInt(tab[9]);
                        } else {
                            INTaveTime = 0;
                        }
                    } catch (NullPointerException e) {
                        System.out.println("averagetime hits faulty");
                    }

                    intDBUP = conn.prepareStatement("insert into integratable(site,hits,kbytes,visits,"
                            + "users,anonUsers,errorsTMP,wap1hits,wap2hits,aveTime,curr_Date)"
                            + "values('" + INTsite + "'," + INThits + "," + INTkbytes + "," + INTvisits + "," + INTusers + "," + INTanonUsers + "," + INTerrors + ","
                            + "" + INTwap1hits + "," + INTwap2hits + "," + INTaveTime + "," + dateFormat.format(calendar.getTime()) + ")");
                    intDBUP.execute();
                    intDBUP.clearBatch();

                }
            }
            intDBUP.close();
            in.close();

        } catch (MalformedURLException ex) {
        } catch (FileNotFoundException fe) {
            System.out.println("The following url is NOT AVAILABLE:::\n " + urlWG);
        } catch (IOException e) {
        }
    }
}

Please note all this information is suppose to be dumped in a mysql database.

your help will be appreciated.

The line numbers in the error message don't match the posted code. Makes it hard to see where/what the error is.
Try debugging your program by adding println()s to show the values as they are processed.
What is the size of the array? Why is an index of 3 causing: ArrayIndexOutOfBoundsException?

What is the size of the tab[] array? Print out the size after it is created

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.