JessB
JessB

Reputation: 55

Error ArrayIndexOutOfBoundsException in Parsing Tab Delimited text file in Java

I have a script that reads a text file. The text that is read in is then used in an SQL WHERE clause to return data from a SQL db.

This works great, but I now have 2 columns of data in my text file, separated by tabs. And I want to use both columns of data in my WHERE clause, but I'm getting an

ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1.

I'm assuming it isn't splitting my tabbed data and for the life of me can't figure out what I'm doing wrong.

Here is my code:

public class Find_ImagePaths {

    public static void main(String[] args) throws IOException, SQLException {
        Connection conn = DriverManager.getConnection(
            "jdbc:mysql://myServer/myTable?useSSL=false", "UserName", "Password");
            
            Statement stmt1 = conn.createStatement(); {
                        
            BufferedReader reader = new BufferedReader(new FileReader("c:\\ImageExport\\SeriesDates.txt"));
            @SuppressWarnings("unused")
            int Counter = 1;
            String line;
            String pid;
            String series;
            while ((line = reader.readLine()) != null) {
                
            @SuppressWarnings({ "resource", "unused" })
            Scanner scanner = new Scanner(line);
               
                String [] token = line.split("\t");
                pid = token[0];
                series = token[1];{
            
            ResultSet rs1 = stmt1.executeQuery("SELECT c.ClientID AS CID, sopi.PATHINSTORAGE, date_format(se.SeriesDate, '%Y-%m-%d') AS SeriesDate, sopi.VISITSUBGROUPVALUE as Type FROM client c LEFT JOIN Study st ON c.ID = st.CLIENT_ID LEFT JOIN Series se ON st.ID = se.STUDY_ID LEFT JOIN SOPINSTANCE sopi ON se.ID = sopi.SERIES_ID WHERE c.ClientID = '"+pid+"' AND date_format(se.SeriesDate, '%Y-%m-%d') = '"+series+"'");

            while (rs1.next() ) {
                BufferedWriter bw = null;
                bw = new BufferedWriter(new FileWriter("c:\\ImageExport\\PathInStorage.txt", true));

                String[] array = new String [4];

                array[0] = rs1.getString("PATHINSTORAGE");
                array[1] = rs1.getString("CID");
                if (rs1.wasNull()) 
                    array[1] = "";
                array[2] = rs1.getString("SeriesDate");
                if (rs1.wasNull()) 
                    array[2] = "";
                array[3] = rs1.getString("Type");
                if (rs1.wasNull()) 
                    array[3] = "";

                bw.write(array[0]);
                bw.write("\t");
                bw.write(array[1]);
                bw.write("\t");
                bw.write(array[2]);
                bw.write("\t");
                bw.write(array[3]);
                    bw.newLine();
                    bw.close();     
            }
        }
    }
    reader.close();
    conn.close();                   
}}}

I am using pid and series in the WHERE clause of my sql statement. And I'm assuming my error is somewhere in here, as this is really the only section of code that I messed with after having the second column of data added to my text file:

 String [] token = line.split("\t");
 pid = token[0];
 series = token[1];

Upvotes: 2

Views: 62

Answers (1)

Ajeet Shah
Ajeet Shah

Reputation: 19863

You can use split(String regex, int limit) method to split by one or more spaces using below regex:

String [] token = line.split("\\s+");

The + quantifier means one or more matches.

Upvotes: 2

Related Questions