2

I am trying to read file and split its line to get some context(Computer Name and Date), the code gives few lines of outputs then gives the following exception:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
        at FILE_MAIN.getComputerName(FILE_MAIN.java:34)
        at FILE_MAIN.readFiles(FILE_MAIN.java:24)
        at FILE_MAIN.main(FILE_MAIN.java:12)

Code:

import java.util.*;
import java.io.*;
import java.util.Scanner;
public class FILE_MAIN
{
    public static void main(String[] args) throws FileNotFoundException
    {
        File folder = new File("folderName/");
        File[] listOfFiles = folder.listFiles();
        for (int i = 0; i < listOfFiles.length; i++)
        {
            readFiles(listOfFiles[i].getName());
        }
    }
    public static void readFiles(String fileName) throws FileNotFoundException
    {
        FileReader dataFile = new FileReader("yukt/"+fileName);
        try (Scanner FileRead = new Scanner(dataFile)) 
        {
            while (FileRead.hasNextLine() && FileRead.nextLine().isEmpty()==false)
            {
                String[] split;
                String line = FileRead.nextLine();
                split = line.split("\\|",-1);
                String computerName=getComputerName(split[0]);
                System.out.println(computerName);
            }      
        }
    }
    public static String getComputerName(String splited)
    {
        String[] split1;
        String[] split2;
        split1=splited.split("\\:",-1);
        split2=split1[1].split("\\ ",-1);
        return("a");
    }
    public static String getDate(String splited)
    {
        String[] split1=splited.split("\\(",-1);
        String[] split2=split1[1].split("\\ ",-1);
        return(split2[0]);
    }
}

The main function gets names of the files in a folder, and passes each file to the readFiles function where each line is split into 3 parts by a delimeter(|) and parts are send to getComputerName and getDate function which returns its values after further splitting the strings.

Here is an example of a line of the file, all the lines are similar to this:

[CD8C] ComputerName:NITIN UserID:GO ankurjain Station 9900  LanId: | (11/24 19:50:30) | Client is disconnected from agent.
1
  • Add an example-input to your question. Commented Mar 25, 2012 at 11:08

4 Answers 4

4

There is no protection for split1 containing a single element:

split1=splited.split("\\:",-1);
split2=split1[1].split("\\ ",-1); // Accesses second element of split1

Add protection and decide if it is an error for there to be no : in the input string or just use whole string if no ::

split1=splited.split("\\:",-1);
if (split1.length > 1)
{
    split2=split1[1].split("\\ ",-1);
} 
Sign up to request clarification or add additional context in comments.

Comments

0
split1=splited.split("\\:",-1);
split2=split1[1].split("\\ ",-1);

split1 must not be doing what you think. i.e. it is not splitting, cos split1[1] is not valid.

You should really check the result of the first split before trying to use it's results.

Comments

0

I had similar issue where I had to check weather string sub part contains given string or not. But String in question had many variation. Instead of using if loop I used ternary operator -

StringUtils.containsIgnoreCase("Test String",
                    ("split me".split(":").length > 1)
                        ? "split me".split(":")[1] : "Can't split")

Comments

-1

split2=split1[1] gives you java.lang.ArrayIndexOutOfBoundsException: 1 the error. The Array does not have 2 elements so index on 1 will throw an error.

You could add a check to make sure it has atleast 2 elements by putting the assignement in a if statement

if (split1.lenght > 1){
  split2=split1[1].split("\\ ",-1);
}  

1 Comment

Assume we have this string String str="call foo" splitting this string based on "\t" will result in two separated values, but the str.length will not be greater than 1. therefore your answer, I think, will not work correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.