This content has been marked as final.
Show 20 replies
-
15. Re: Parsing a date error.help with small program
3004 Oct 12, 2009 11:01 PM (in response to 843789)Implode wrote:
There's no way to know why you get that error when all you show is that pattern.
I tried this, but I guess it doesnt check if the values fit into day month year
pattern = Pattern.compile("\\d{2}\\d{2}\\d{2}");
But i still get an array out of bounds error
As mentioned before: Provide an SSCCE. Include the exact, complete error message and indicate clearly exactly which line is causing it. -
16. Re: Parsing a date error.help with small program
3004 Oct 12, 2009 11:04 PM (in response to 843789)Implode wrote:
[http://www.google.com/search?q=java+simpledateformat+example]
Our posts are crossing.
:(
I have looked at all methods mentioned by members on this topic, but Im new to this and have no idea how to validate using simpleDateFormat.
So maybe it is better to use try and catch after every token from the split string?
I have no idea what you're talking about.
But how could I tell the method that if an exception is found add the line to errorLine array, if no catch found then add to goodLine array?
try { do something good.add(something) } catch (...) { errors.add(something); } try { do something else good.add(something else) } catch (...) { errors.add(something else); }
-
17. Re: Parsing a date error.help with small program
843789 Oct 12, 2009 11:18 PM (in response to 3004)This is where the error is
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7String[] newToken = line.split("#"); pattern = Pattern.compile("ISBN\\x20(?=.{13}$)\\d{1,5}([- ])\\d{1,7}\\1\\d{1,6}\\1(\\d|X)$"); Matcher matchIsbn = pattern.matcher(newToken[0]); pattern = Pattern.compile("[\\d]+"); Matcher matchCopyNumber = pattern.matcher(newToken[1]); pattern = Pattern.compile("[\\D&&\\w]+"); Matcher matchTitle = pattern.matcher(newToken[2]); pattern = Pattern.compile("[\\D&&\\w]+"); Matcher matchAuthor = pattern.matcher(newToken[3]); pattern = Pattern.compile("[\\d]+"); Matcher matchStatistics = pattern.matcher(newToken[4]); pattern = Pattern.compile("\\d{2}\\d{2}\\d{2}"); Matcher matchBorrowDate = pattern.matcher(newToken[5]); pattern = Pattern.compile("\\d{2}\\d{2}\\d{2}"); Matcher matchReturnDate = pattern.matcher(newToken[6]); pattern = Pattern.compile("[\\d]+"); Matcher matchLibraryCardNumber = pattern.matcher(newToken[7]);
at assignment2Program1.WriteFromBookFile.main(WriteFromBookFile.java:54)
Java Result: 1
the first line read from the file only has 7 tokens, token[6]
0-321-26979-0#1#Java Software Solutions#9#080830#081130#90
Edited by: Implode on Oct 12, 2009 4:18 PM -
18. Re: Parsing a date error.help with small program
3004 Oct 13, 2009 5:30 AM (in response to 843789)How well do you understand Java arrays?
[http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html]
The error message is pretty clear. It's telling you exactly what's wrong and exactly where. On line 54 of WriteFromBookFile.java, you're trying to access the 8th element (at index 7) of an array that has at most 7 elements (at indices 0..6).
You're assuming that split is giving you 8 or more tokens, but it's giving you 7 or fewer. So print out each of the elements of the split result.
EDIT: And from your comments, it seems that you know that, so where is the confusion? Why, if you know that there are only elements 0..6, are you trying to access index 7? And why are you surprised that it doesn't work?
Edited by: jverd on Oct 12, 2009 10:30 PM -
19. Re: Parsing a date error.help with small program
843789 Oct 13, 2009 8:37 AM (in response to 3004)jverd wrote:
I understand arrays, but I am trying to explain the following.
How well do you understand Java arrays?
[http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html]
The error message is pretty clear. It's telling you exactly what's wrong and exactly where. On line 54 of WriteFromBookFile.java, you're trying to access the 8th element (at index 7) of an array that has at most 7 elements (at indices 0..6).
You're assuming that split is giving you 8 or more tokens, but it's giving you 7 or fewer. So print out each of the elements of the split result.
EDIT: And from your comments, it seems that you know that, so where is the confusion? Why, if you know that there are only elements 0..6, are you trying to access index 7? And why are you surprised that it doesn't work?
Edited by: jverd on Oct 12, 2009 10:30 PM
I need to read information from a text file.
e.g. lines in file
0-321-26979-9#4#Java Software Solutions#9#071002#071423#26
0-13-089496-6#2#Business and information systems#Nickerson#15#000506
0-13-089496-6#4#Business and information systems#Nickerson#15#000506#081109#92
Each can have a possible 8 fields.
Most of the lines don't, which is giving an array out of bounds error.
That is what I would like to know, how can it be coded so that if there arent all the fields in a line, it moves to the next.
A few more problems arise.
Firstly, some of the information spans over 2 lines, so I cannot use nextLine().
Secondly, different "lines" contain different fields. which means that i.e. token 4 in line 3 will be a different field to token 4 in line 6.
It seems like the only way to correctly check each "group" of fields , is to check if the first token contains a 0 at the first char. but that doesnt solve the fact that different lines have fields in different position, so it cannot be matched?
Does this make sense now? -
20. Re: Parsing a date error.help with small program
3004 Oct 13, 2009 3:24 PM (in response to 843789)Implode wrote:
If you understand arrays, then you know how to do this. It's simply a matter of making sure an element actually exists before trying to access it. If you can't figure out how to do that, go back through the arrays tutorial and see if it comes to you.
I understand arrays, but I am trying to explain the following.
I need to read information from a text file.
e.g. lines in file
0-321-26979-9#4#Java Software Solutions#9#071002#071423#26
0-13-089496-6#2#Business and information systems#Nickerson#15#000506
0-13-089496-6#4#Business and information systems#Nickerson#15#000506#081109#92
Each can have a possible 8 fields.
Most of the lines don't, which is giving an array out of bounds error.
That is what I would like to know, how can it be coded so that if there arent all the fields in a line, it moves to the next.