0

I have a file that has over 80k rows of that data that im parsing through and inserting into a table. the file is pipe delimited and I only want the first 3 fields for each row. when I tested out the spring boot program i made It worked with 10 lines of data fine, but when I tried to do it with a real file I got a ArrayIndexOutofBoundExcepetion: 1 at my String sql variable. I believe that there is to much data im trying to store in the list.

I need help to change my code to read one line at a time and then insert into the database until all lines in the file have been covered. I think spring batch would help me do this but I'm not familiar with it and don't how to edit my code to do the line by line inserting with my current knowledge and experience.

normx222
  • 25
  • 7
  • can you share the exception's stackTrace that printed out the console/logfile please? –  Mar 26 '20 at 20:55
  • @AhmetOZKESEK i edited for stack trace – normx222 Mar 26 '20 at 21:46
  • 1
    I don't see why you removed the code and stack trace. They were helpful. It looks like your code is puking when you `val[1]` in the construction call. Can you tell which row is having the problem? If it's the first row, then your `split` call isn't working properly and you should go look at the question marked as a duplicate, closely. If not, then some of your data isn't following the pattern, and you need to build safeguards. I'd build them in anyway. Because it's not throwing for `val[0]`, I suspect that you're just not parsing the data out properly. `println` is your friend. – Mark Storer Apr 10 '20 at 13:49

2 Answers2

0

looks like your input file is problematic. check your input file, seems like for one row (or more), it is having one or less "|" (you splitting string on |) . OR check do you have few additional new line characters in the end of file.

Ben
  • 54
  • 4
  • I scrolled through all the lines and each one has multiple "|" but I do have ending in "| | |N|" is that possible why? – normx222 Mar 27 '20 at 14:43
  • your code expecting array of 3 elements....if any line returning less than 3 you will get index out of Bound exception. – Ben Mar 27 '20 at 15:40
  • every single line will have more then just 3 fields but I only want the first 3 fields – normx222 Mar 27 '20 at 16:11
0

Its simple. You need to escape pipe character with two back slashes. Try replacing:

s.split("[|]"...

with:

s.split("[\\|]"...

Pipe alone is treated as meta/regex in your situation.

Ajay Kumar
  • 2,906
  • 3
  • 23
  • 46
  • im stil getting the same error only this time its at `.map(s -> s.split("[|]")).map(val -> new ZygateEntity(val[0],val[1],val[2])).collect(Collectors.toList());` – normx222 Mar 27 '20 at 13:35