0

So, I'm reading from a txt file. The file is something like this, separate with a tab "/t"

 ID    name    test    date    object
 1     John    pass    9/13    RC_CUST_1
 2     John    fail    10/13   RC_CUST_1
 3     John    pass                    

I'm reading the txt file like this:

String line="";
ArrayList<String> data = new ArrayList<String>();
FileReader fIn;
BufferedReader buff;

fIn = new FileReader("...");
buff = new BufferedReader( fIn );

while( ( line = buff.readLine() ) != null  )
{
        if(!line.isEmpty())
            data.add(line);
}

In late of my code I do that:

    ArrayList<String> name1  = new ArrayList<String>();
    ArrayList<String> RC1  = new ArrayList<String>();
    ArrayList<String> date1  = new ArrayList<String>();
    ArrayList<String> pass1 = new ArrayList<String>();
    String [] substr2;

for(int k=0;k<data.size();k++)
{
    substr2=data.get(k).split("\t");
    RC1.add(substr2[4]);
    name1.add(substr2[1]);
    date1.add(substr2[3]);
    pass1.add(substr2[2]);      
}

And I have an error because you can see, my ID number 3 has not any date or object. How can I fix that, and how can I know what ID has not a date or an object

I would like for example to print: ID number x has not date or ID number x has not any object if I have a error of this type

5
  • Which error are you getting? Commented Sep 13, 2019 at 8:30
  • Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 Commented Sep 13, 2019 at 8:32
  • Your tab delimited seems to be malformed, because an empty field not should mean that any tabs are actually missing. The best solution is to go back to your data source and fix it there. Commented Sep 13, 2019 at 8:33
  • Isn't it quite error prone to split by "\t"? I would use an arbitrary amount of whitespaces instead: .split("\\s+");... Consider creating a class representing a person or a test (depends) representing your data, a bunch of ArrayLists might get hard to handle later on. Also consider using java.nio to access file content, you can simply stream the lines by java.nio.file.Files.lines(java.nio.file.Path filePath);. Commented Sep 13, 2019 at 8:34
  • The error it's not about "\t". I get the error only if a field it's empty. I mean the error is when there when I try to add the substr2[x] in ArrayList Commented Sep 13, 2019 at 10:12

1 Answer 1

1

After the split on \t you get an array as result. you can check the length of the array before accessing the fields in it.

for(int k=0;k<data.size();k++) {
    String [] substr2 = data.get(k).split("\t");
    int length = substr2.length
    if (length >= 5) {
        RC1.add(substr2[4]);
    } else {
        System.out.println("no object found");
    }
    ....     
}
Sign up to request clarification or add additional context in comments.

Comments

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.